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
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');
$curl = curl_init('http://thecodeprogram.com');
You can also create and send other configuration settings as above.
$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);
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.
// 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:
// 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…
Comments
I am not sure where you’re getting your info, but great topic. I needs to spend some time learning more or understanding more. Thanks for great info I was looking for this information for my mission.
2022/08/17 17:43:40