Home >Backend Development >PHP Tutorial >PHP Master | Using cURL for Remote Requests
<span><span><?php </span></span><span><span>// init the resource </span></span><span><span>$ch = curl_init(); </span></span><span> </span><span><span>// set a single option... </span></span><span><span>curl_setopt($ch, OPTION, $value); </span></span><span><span>// ... or an array of options </span></span><span><span>curl_setopt_array($ch, array( </span></span><span><span>OPTION1 => $value1, </span></span><span><span>OPTION2 => $value2 </span></span><span><span>)); </span></span><span> </span><span><span>// execute </span></span><span><span>$output = curl_exec($ch); </span></span><span> </span><span><span>// free </span></span><span><span>curl_close($ch);</span></span>The only thing that changes for the request is what options are set, which of course depends on what you’re doing with cURL.
<span><span><?php </span></span><span><span>curl_setopt_array( </span></span><span><span>$ch, array( </span></span><span><span>CURLOPT_URL => 'http://www.bbc.co.uk/', </span></span><span><span>CURLOPT_RETURNTRANSFER => true </span></span><span><span>)); </span></span><span> </span><span><span>$output = curl_exec($ch); </span></span><span><span>echo $output;</span></span>Check the output in your browser and you should see the BBC website displayed. We’re lucky as the site displays correctly because of its absolute linking to stylesheets and images. The options we just used were:
<span><span><?php </span></span><span><span>// init the resource </span></span><span><span>$ch = curl_init(); </span></span><span> </span><span><span>// set a single option... </span></span><span><span>curl_setopt($ch, OPTION, $value); </span></span><span><span>// ... or an array of options </span></span><span><span>curl_setopt_array($ch, array( </span></span><span><span>OPTION1 => $value1, </span></span><span><span>OPTION2 => $value2 </span></span><span><span>)); </span></span><span> </span><span><span>// execute </span></span><span><span>$output = curl_exec($ch); </span></span><span> </span><span><span>// free </span></span><span><span>curl_close($ch);</span></span>The new options are:
<span><span><?php </span></span><span><span>curl_setopt_array( </span></span><span><span>$ch, array( </span></span><span><span>CURLOPT_URL => 'http://www.bbc.co.uk/', </span></span><span><span>CURLOPT_RETURNTRANSFER => true </span></span><span><span>)); </span></span><span> </span><span><span>$output = curl_exec($ch); </span></span><span><span>echo $output;</span></span>The new options are:
<span><span><?php </span></span><span><span>$postData = array( </span></span><span><span>'login' => 'acogneau', </span></span><span><span>'pwd' => 'secretpassword', </span></span><span><span>'redirect_to' => 'http://example.com', </span></span><span><span>'testcookie' => '1' </span></span><span><span>); </span></span><span> </span><span><span>curl_setopt_array($ch, array( </span></span><span><span>CURLOPT_URL => 'http://example.com/wp-login.php', </span></span><span><span>CURLOPT_RETURNTRANSFER => true, </span></span><span><span>CURLOPT_POST => true, </span></span><span><span>CURLOPT_POSTFIELDS => $postData, </span></span><span><span>CURLOPT_FOLLOWLOCATION => true </span></span><span><span>)); </span></span><span> </span><span><span>$output = curl_exec($ch); </span></span><span><span>echo $output;</span></span>Note that there aren’t many public FTP servers that allow anonymous uploads and downloads for security reasons, so the URL and credentials above are just place-holders. This is almost the same as sending an HTTP request, but only a couple minor differences:
<span><span><?php </span></span><span><span>curl_setopt_array($ch, array( </span></span><span><span>CURLOPT_URL => 'http://example.com/wp-login.php', </span></span><span><span>CURLOPT_RETURNTRANSFER => true, </span></span><span><span>CURLOPT_POST => true, </span></span><span><span>CURLOPT_POSTFIELDS => $postData, </span></span><span><span>CURLOPT_FOLLOWLOCATION => true, </span></span><span><span>CURLOPT_COOKIESESSION => true, </span></span><span><span>CUROPT_COOKIEJAR => 'cookie.txt' </span></span><span><span>));</span></span>The important options here are:
<span><span><?php </span></span><span><span>// init the resource </span></span><span><span>$ch = curl_init(); </span></span><span> </span><span><span>// set a single option... </span></span><span><span>curl_setopt($ch, OPTION, $value); </span></span><span><span>// ... or an array of options </span></span><span><span>curl_setopt_array($ch, array( </span></span><span><span>OPTION1 => $value1, </span></span><span><span>OPTION2 => $value2 </span></span><span><span>)); </span></span><span> </span><span><span>// execute </span></span><span><span>$output = curl_exec($ch); </span></span><span> </span><span><span>// free </span></span><span><span>curl_close($ch);</span></span>The above code took around 1,100 ms to execute on my laptop. Performing the requests sequentially without the multi interface it took around 2,000 ms. Imagine what your gain will be if you are sending hundreds of requests! Multiple projects exist that abstract and wrap the multi interface. Discussing them is beyond the scope of the article, but if you’re planning to issue multiple requests asynchronously then I recommend you take a look at them:
<span><span><?php </span></span><span><span>// init the resource </span></span><span><span>$ch = curl_init(); </span></span><span> </span><span><span>// set a single option... </span></span><span><span>curl_setopt($ch, OPTION, $value); </span></span><span><span>// ... or an array of options </span></span><span><span>curl_setopt_array($ch, array( </span></span><span><span>OPTION1 => $value1, </span></span><span><span>OPTION2 => $value2 </span></span><span><span>)); </span></span><span> </span><span><span>// execute </span></span><span><span>$output = curl_exec($ch); </span></span><span> </span><span><span>// free </span></span><span><span>curl_close($ch);</span></span>If an error pops up, you can check it out with curl_error():
<span><span><?php </span></span><span><span>curl_setopt_array( </span></span><span><span>$ch, array( </span></span><span><span>CURLOPT_URL => 'http://www.bbc.co.uk/', </span></span><span><span>CURLOPT_RETURNTRANSFER => true </span></span><span><span>)); </span></span><span> </span><span><span>$output = curl_exec($ch); </span></span><span><span>echo $output;</span></span>
cURL, or Client URL, is a library that allows you to make HTTP requests in PHP. It’s used to communicate with different types of servers and to download or upload data. cURL supports various protocols like HTTP, HTTPS, FTP, and more. It’s a powerful tool that can be used to interact with APIs, scrape web pages, or even download files.
cURL is usually included in most web servers. However, if it’s not enabled, you can do so by modifying your PHP.ini file. Locate the line that says “;extension=curl” and remove the semicolon. If the line doesn’t exist, you can add it at the end of the file. After making changes, save the file and restart your web server.
To make a simple cURL request, you first need to initialize cURL with the curl_init() function. Then, set your options using the curl_setopt() function. Finally, execute the request with curl_exec() and close the session with curl_close(). Here’s a basic example:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
You can handle errors in cURL by using the curl_errno() and curl_error() functions. These functions return the last error number and error message respectively. Here’s an example:
if(curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
To send a POST request, you need to set the CURLOPT_POST option to true and the CURLOPT_POSTFIELDS option to an array of data you want to send. Here’s an example:
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "postvar1=value1&postvar2=value2");
You can set custom headers by using the CURLOPT_HTTPHEADER option. This option takes an array of headers as its value. Here’s an example:
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer ' . $token
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
To follow redirects, you need to set the CURLOPT_FOLLOWLOCATION option to true. Here’s how you can do it:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
To get the response headers, you can set the CURLOPT_HEADER option to true. This will include the headers in the output. Here’s an example:
curl_setopt($ch, CURLOPT_HEADER, true);
To send a file, you can use the CURLOPT_POSTFIELDS option and prefix the file path with an @ symbol. Here’s an example:
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => '@/path/to/file.txt'));
To use cURL with a proxy, you can set the CURLOPT_PROXY option to the address of the proxy. Here’s how you can do it:
curl_setopt($ch, CURLOPT_PROXY, "http://proxy.example.com:8080");
The above is the detailed content of PHP Master | Using cURL for Remote Requests. For more information, please follow other related articles on the PHP Chinese website!