How to Use CURL with PHP

How to Use CURL with PHP

Hi everyone I will talk about How can we use CURL in PHP. We can use the CURL to make some requests to other webpages via http and get some datas from requested pages.

CURL is a powerful web engine to carry data through the web protocols. It is a system for transferring data between web sites so that it can be logged in the stretched places or clear any api. With CURL, you can do everything from simple http operations to very complex ftp operations. Sending the value with POST or GET is routed very quickly in a very complex way.

Now take a look at how can we use CURL basically :

We need to start CURL before we can do anything about CURL. After we get started, we can do what we want. Curling library curl_init(); Function. We call this function and do our operations. Then we terminate CURL.

The CURL library has some configuration settings. Now let's take a look at those settings

  • CURLOPT_RETURNTRANSFER : Sets how the data returns to the sender.
  • CURLOPT_CONNECTTIMEOUT :  Specify a timeout for the connection
  • CURLOPT_TIMEOUT :         Maximum time for CURL process
  • CURLOPT_USERAGENT :       If you want to go to the target site with a user Agent, you must fill here
  • CURLOPT_URL  :            Target URL
  • CURLOPT_POST :            You can use POST method to transfer data
  • CURLOPT_POSTFIELDS :      You can set the post fields of the request.

Now that we have this information, let's make examples and reinforce the CURL.


$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://thecodeprogram.com');
In the example above, we created a curl file and made thecodeprogram.com the url where the transaction will be performed. We can do the same process as follows. As a result, it will not be a problem because it is a target.
$curl = curl_init('http://thecodeprogram.com');

You can also create and send other configuration settings as above.

There is another way to send data. I have showed it below:

$curl = curl_init();
curl_setopt_array($curl, array(
     CURLOPT_RETURNTRANSFER => 1,
     CURLOPT_URL => 'http://thecodeprogram.com'
));

As you can see above, we have sent the values we want to send into the CURL object that we created as an array and made it workable.
Well, we've prepared the data we're going to send, and I'm hearing you say we won't talk about how to send them. Now let's look at it.

We run the CURL object that we created with the curl_exec () method and wait for the operations we want. When it completes, it returns us a value of true or false, and we understand whether or not it has been processed according to this value.

$result = curl_exec($curl);
It is executed as above and is thrown to the $result variable.
The returned value:
  • If true process is succesfully.
  • Else if false there is error during the process and it failed.

If the result comes from above, the result is the data within the site. For example, a JSON or XML or CSV-style data can be. We read this data and do the operation we want.

After we finish our operation, you can use curl_close(); function.

// cURL initing
$curl = curl_init();
// some configurations
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://thecodeprogram.com/',
    CURLOPT_USERAGENT => 'Mozilla Firefox 5.0'
));
// start the cURL process
$resp = curl_exec($curl);
// And finish the cURL session
curl_close($curl);

Now take a look at the posting data with cURL:

To post data, we'll turn on the post feature in the configuration settings, and we'll create and send the data we post.

// initing cURL
$curl = curl_init();
// Make some configurations here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://thecodeprogram.com',
    CURLOPT_USERAGENT => 'Mozilla Firefox 9.0',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        username=> 'burakhamdi',
        password=> '123456'
    )
));
// starting the cURL created above
$resp = curl_exec($curl);
// and finish the cURL session
curl_close($curl);
So if you ask if there is a code for any errors, we can check them as follows.
if(!curl_exec($curl)){
    die('Error : "' . curl_error($curl) . '" - Error Code : ' . curl_errno($curl));
}

In the above way, we can have the errors checked.
We talked about what is CURL as simple as this and what does the job. PHP is one of my biggest blessings to me. That's why I recommend you to use it and learn to go deeper.

Thats all in this article.

Keep following me.

Have a nice coding.

Burak Hamdi TUFAN…


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment.

Comments

  • There is no comment. Be the owner of first comment...