Requirements
To be able to use Curl you must install on server libcurl package. Depending on what PHP version you have on server you need to install the appropriate libcurl version like:
- PHP 4.2.3 requires libcurl version 7.9.0
- PHP 4.3.0 requires librucl version 7.9.8
- PHP 5.0.0 requires a librucl version 7.10.5
PHP Curl GET Method
Below is simple function for sending GET request using Curl. We send search request to Google and get as return matching results.
- $url = 'https://www.google.com/search';
-
- $data = array (
- 'q' => 'nokia'
- );
-
- $params = '';
- foreach($data as $key=>$value)
- $params .= $key.'='.$value.'&';
-
- $params = trim($params, '&');
-
- $ch = curl_init();
-
- curl_setopt($ch, CURLOPT_URL, $url.'?'.$params ); //Url together with parameters
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Return data instead printing directly in Browser
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 7); //Timeout after 7 seconds
- curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
- curl_setopt($ch, CURLOPT_HEADER, 0);
-
- $result = curl_exec($ch);
- curl_close($ch);
-
- if(curl_errno($ch)) //catch if curl error exists and show it
- echo 'Curl error: ' . curl_error($ch);
- else
- echo $result;
In '$data' array we can have as many parameters as we want, in our case only 1 is needed. Let's analyze the options in this request: curl_setopt($ch, CURLOPT_URL, $url.'?'.$params ) - Remote URL where we want to send the request curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) - To store the result in variable you must set this option to 1 or true because if you don't the results will be printed directly in browser. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 7)- Using timeout we protect our server from performing long requests if something goes wrong. curl_setopt($ch, CURLOPT_HEADER, 0) - Because we store our result in a variable we set this option to 0 because we don't want also to get the header information that we receive.
PHP Curl POST Method
To send POST request using curl to specific remote location we will modify above code and add extra curl options.
- $url = 'http://www.domain.com';
-
- $data = array (
- 'key1' => 'value1',
- 'key2' => 'value2',
- 'key3' => 'value3'
- );
-
- $params = '';
- foreach($data as $key=>$value)
- $params .= $key.'='.$value.'&';
-
- $params = trim($params, '&');
-
- $ch = curl_init();
-
- curl_setopt($ch, CURLOPT_URL, $url); //Remote Location URL
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Return data instead printing directly in Browser
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 7); //Timeout after 7 seconds
- curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
- curl_setopt($ch, CURLOPT_HEADER, 0);
-
- //We add these 2 lines to create POST request
- curl_setopt($ch, CURLOPT_POST, count($data)); //number of parameters sent
- curl_setopt($ch, CURLOPT_POSTFIELDS, $params); //parameters data
-
- $result = curl_exec($ch);
- curl_close($ch);
-
- echo $result;
As you can see from previous GET Request php code there are 2 more lines in above code: curl_setopt($ch, CURLOPT_POST, count($data)) - We set the number of posted parameters curl_setopt($ch, CURLOPT_POSTFIELDS, $params) - We add here the POST parameters separately from the main url.
Other Posts Yo