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

Need help with Linux Server or WordPress? We can help!

Leave a Reply

Your email address will not be published. Required fields are marked *