PHP cURL example
Accessing an API though PHP cURL.
In this post we will be creating a simple PHP cURL request.
For example, you have acccess to some API and you want your application to programatically access it. Or just simple want to get the contents of a specific page.
Quick Explanation
Here are some explanation of function that we will be using
Creates a cURL Handler to be used in the request.
- url [Optional] - The URL where we are sending a request to.
Returns a cURL handler.
Sets options for the cURL handler
- cURLHandler [Required] - The cURL handler.
- curl_option [Required] - Options for the request.
- curl_option_value [Required] - The Value for the option.
For a list of valid
curl_options and its values
cURL Options.
Returns Boolean.
Performs/Executes the request.
- cURLHandler [Required] - The cURL handler.
Returns Boolean or depending on the options it can return the contents of the page.
Closes the our hander and removes resources used in the request.
- cURLHandler [Required] - The cURL handler.
Get's the response info of the request.
- cURLHandler [Required] - The cURL handler.
Returns an Associative Array.
Important values that we are going to use in the info of
curl_getinfo ( cURLHandler ).
- url - The URL we sent the request.
- http_code - The status code of our request. So we know that our request was successful or not.
Example:
GET Request
$urlToSendTo = "https://example.com/";
// create a cURLHandler
$ch = curl_init( );
// sets the url to the cURLHandler options
curl_setopt( $ch, CURLOPT_URL, $urlToSendTo );
// we want the content of this page
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// executes the request and saves the output to a variable
$theContent = curl_exec ( $ch );
// close
curl_close ( $ch );
Now we have a simple PHP cURL request that get the contents of https://example.com/ and save it to $theContent
But what if the page you want needs a form submittion to be accessed?
Here's the curl_option that we are going to use.
POST Request
// your POST values example
$form = [
'username' => 'JohnDoe',
'password' => '1234567890'
];
$urlToSendTo = "https://example.com/";
// create a cURLHandler
$ch = curl_init( );
// sets the url to the cURLHandler options
curl_setopt( $ch, CURLOPT_URL, $urlToSendTo );
// we want the content of this page
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE);
// tells that we have X numbers of post to send
curl_setopt( $ch, CURLOPT_POST, count($form));
// sets the values of post to our request
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query($form));
// executes the request and saves the output to a variable
$theContent = curl_exec ( $ch );
// close
curl_close ( $ch );
If you are creating/sending to a REST API a "PUT" and "DELETE" request will also be useful.
PUT Request
// your POST values example
$form = [
'username' => 'JohnDoe',
'password' => '1234567890'
];
$urlToSendTo = "https://example.com/";
// create a cURLHandler
$ch = curl_init( );
// sets the url to the cURLHandler options
curl_setopt( $ch, CURLOPT_URL, $urlToSendTo );
// we want the content of this page
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE);
// we tell this is a PUT request
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
// sets the values of post to our request
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($form) );
// executes the request and saves the output to a variable
$theContent = curl_exec ( $ch );
// close
curl_close ( $ch );
DELETE Request
// your POST values example
$form = [
'id' => '1'
];
$urlToSendTo = "https://example.com/";
// create a cURLHandler
$ch = curl_init( );
// sets the url to the cURLHandler options
curl_setopt( $ch, CURLOPT_URL, $urlToSendTo );
// we want the content of this page
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE);
// we tell this is a DELETE request
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
// sets the values of post to our request
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($form) );
// executes the request and saves the output to a variable
$theContent = curl_exec ( $ch );
// close
curl_close ( $ch );
Tips:
- PHP cURL cannot process JavaScript in the content returned. So if the content of the web page is created from JavaScript it wont be included.
- There a lot of the curl_options to be used. Like for example changing the User Agent, changing the IP Address and many more.
Replies (0)
Reply