Tag: curl

  • How to get Contents of a Webpage using PHP curl

    How to get Contents of a Webpage using PHP curl

    PHP cURL module allows you to connect to remote websites and API endpoints to exchange data. To see if you have php-curl enabled, you can check phpinfo() page or run the command “php -m’ and look for curl.

    boby@sok-01:~$ php -m | grep curl
    curl
    boby@sok-01:~$ 
    

    If php-curl module is not installed, you can install it on Ubuntu/Debian with the command

    apt-get install php-curl
    

    To access the content of a remote web server or API endpoint, you can use the following PHP code.

    
    

    To make script work with invalid or self signed SSL, add following

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    

    If you want to make a POST request, you can use the following code

     'Yujin Boby',
        'email' => '[email protected]'
    );
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 11.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3');
    $response = curl_exec($ch);
    curl_close($ch);
    

    Back to PHP

  • CURL ERROR 7 could not establish a secure connection to WordPress.org

    CURL ERROR 7 could not establish a secure connection to WordPress.org

    On a WordPress website hosted on a CentOS server, I got the following error on the header of the website.

    Warning: An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the support forums. (WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.) in /var/www/html/wp-admin/includes/translation-install.php on line 68

    On debugging the code, I found the code returned “CURL ERROR 7”. This happens when the curl connection is blocked. I was able to curl to the URL from the command line.

    curl https://api.wordpress.org/translations/core/1.0/
    

    To error is due to SELinux. To fix the error, run the command

    setsebool -P httpd_can_network_connect on
    

    Or disable SELinux.

    See SELinux, WordPress.

  • How to use HTTP Basic authentication with cURL

    To access sites that use HTTP Basic authentication with curl, use command

    curl -u USERNAME_HERE:PASSWORD_HERE http://domain.extn
    

    Or

    curl -u USERNAME_HERE http://domain.extn
    

    If you don’t specify password, curl will promt for password.

    See curl

  • curl using cookie

    Here is some example of using curl to login to site, use cookie to do further requests.

    curl -k --cookie-jar ./cookies_hotfile --data "returnto=%2F&user=USERNAME_HERE&pass=PASSWORD_HERE" http://site.com/login.php
    curl -L -O --cookie ./cookies_hotfile http://site.com/dl/5222/4444/file.zip.html
    

    See curl

  • Fetch as Google with curl

    A hacked web site got hacked. The site listed on google SERP with text “There are essentially two prescribed drugs which viagra 200mg”.

    Hacked site on google SERP

    When you visit the site in the browser and check the source, you see everything normal, no text related to Viagra. This is because malware on the site only shows that content to Google bot.

    To see the malware inserted text, you can use Fetch as Google tool in your Google webmaster center. But before using the fetch as Google tool, you need to verify ownership of the site.

    Another way is to change your browsers user agent to same as Google bot

    Googlebot/2.1 (+http://www.google.com/bot.html)

    You can see list of user agents used by google at

    https://support.google.com/webmasters/answer/1061943?hl=en

    To Fetch as site as google using curl, run

    curl --user-agent "Googlebot/2.1 (+http://www.google.com/bot.html)" https://your-site-here.extn

    Back to curl

  • Check if HTTP/2 enabled using curl

    To see if a web site have HTTP/2 enabled, you can run

    curl -sI SITE_URL_HERE -o /dev/null -w '%{http_version}\n'
    

    If you see 2, the site have HTTP/2 enabled.

    Example

    boby@sok-01:~$ curl -sI https://serverok.in/ -o /dev/null -w '%{http_version}\n'
    2
    boby@sok-01:~$ 
    

    Here is a site with no HTTP/2

    boby@sok-01:~$ curl -sI http://ok.serverok.in/ -o /dev/null -w '%{http_version}\n'
    1.1
    boby@sok-01:~$ 
    
  • Making POST request from curl

    Here is an example POST request using curl

    curl -d "param1=value1¶m2=value2" -X POST http://sok.test/post.php
    

    Sending JSON string

    curl  http://video.com/like -X POST --data '{"like":"1","media_id":"781","time_start":"100:36:19.584"}'
    

    Post a variable

    curl  URL_HERE -X POST --data 'badSSN=111111111'
    
  • Using Host Header in curl

    When you have multiple websites hosted on a server, if you want to access one of the site using curl to IP of the server, then you need to pass the Host header value.

    For example

    curl --header "Host: www.mydomain.com" SERVER_IP_HERE

    If you want to show debug info, add –verbose

    curl --verbose --header "Host: www.mydomain.com" SERVER_IP_HERE

    Back to curl

  • curl

    View Header

    To view the response header, use

    curl -I URL_HERE

    Specify Host Header

    Example

    curl http://192.99.201.92/ip.php -H "host: iptools.bizhat.com"

    Using Proxy

    To connect to a proxy using an Authenticated proxy, use

    curl -U PROXY_USER:PROXY_PW -x MY_PROXY_SERVER:PROXY_PORT http://checkip.amazonaws.com

    Proxy with IP auth example

    curl -x http://207.66.90.22:16416 -L http://checkip.amazonaws.com

    To connect using Socks5 proxy running on 127.0.0.1 port 7070, run

    curl --socks5 127.0.0.1:7070 http://checkip.amazonaws.com

    For socks proxy with authentication, use

    curl --socks5 USER_HERE:PW_HERE@PROXY_IP:1080 http://checkip.amazonaws.com

    Sent Multiple Rquests

    Following command will sent 100 requet to url specified.

    curl -s "http://serverok.in/wp-login.php?[1-100]"

    On the server, you will see requests like

    http://serverok.in/wp-login.php?1
    http://serverok.in/wp-login.php?2
    http://serverok.in/wp-login.php?3