Logo
Code Ranks ×

PHP cURL (Getting Content Of Any URL)|coderanks

02/04/2019  .   2 minutes, 18 seconds to read  .   Posted by Admin
#php #curl #phpcurl #learnphp #phpframework #phpcrud #phptutorial #phpforms #phpcode #phpfunctions #phpcodeigniter #phpprogramming #database

What is php curl ?

cURL is a PHP library that is used to call different URLs using different methods(GET, POST, DELETE etc) and protocols. This library is very helpful when we need to load URLs programmatically to perform different actions.

Following is a set of use cases for which we can use PHP cURL.

  1. Getting Content Of Any URL
  2. File Downloading
  3. Checking URL existence
  4. Form Submission
  5. Authentication
  6. Cookies saving and sending

In this part of the article, we'll cover the first point to download the content of any URL.

Getting Content Of Any URL 

Content is anything that is visible when we open any URL. We can download that content programmatically by using cURL. We use this normally in scraping or API Calling. Here is a basic example of content downloading.

<?php
    $curl=curl_init();
    curl_setopt($curl,CURLOPT_URL,'http://www.coderanks.com');
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
    $response = curl_exec($curl);
    curl_close($curl);
    echo $response;
?>

Explanation of PHP cURL Common Functions

PHP cURL has different functions to add different flags and components in the request. Following are commonly used functions.

curl_init()

This function initializes a new cURL request and returns an object which we can use for further processing. In this case, $curl is an object returned by curl_init(). 

$curl=curl_init();

curl_setopt()

This function is used to add different components in the curl object. It normally takes the following arguments.

  1. Curl Object
  2. Component Key
  3. Component Value

In the above code, we use curl_setopt two times to set URL and Return Transfer flag to 1.

curl_setopt($curl,CURLOPT_URL,'http://www.coderanks.com');
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);

curl_exec()

As the name depicts, this function is used to execute the curl request by taking the curl object as an argument. After execution completes, it'll display the response but if CURLOPT_RETURNTRANSFER is set to 1(true) then it'll return the response as follows.

$response = curl_exec($curl);

curl_close()

After executing curl request, we need to close it by calling curl_close().

curl_close($curl)

Setting all options at once

We don't need to call curl_setopt for each option but we can pass an array of options by using curl_setopt_array. By using this approach, our code will look like this:

<?php
    $curl=curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => "http://www.coderanks.com",
        CURLOPT_RETURNTRANSFER => true,
    ));
    $response = curl_exec($curl);
    curl_close($curl);
    echo $response;
?>
1 vs true in PHP cURL

As in most cases in programming, we can use 1 or true interchangeably in PHP cURL options.

This is all for this 1st part of the article, we'll study File Downloading in the next part. 

Thanks for reading.