martes, 17 de septiembre de 2019

PHP Curl GET Method and POST Method

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.
  1. $url = 'https://www.google.com/search';
  2.     $data = array (
  3.         'q' => 'nokia'
  4.         );
  5.        
  6.         $params = '';
  7.     foreach($data as $key=>$value)
  8.                 $params .= $key.'='.$value.'&';
  9.          
  10.         $params = trim($params, '&');
  11.     $ch = curl_init();
  12.     curl_setopt($ch, CURLOPT_URL, $url.'?'.$params ); //Url together with parameters
  13.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Return data instead printing directly in Browser
  14.     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 7); //Timeout after 7 seconds
  15.     curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
  16.     curl_setopt($ch, CURLOPT_HEADER, 0);
  17.    
  18.         $result = curl_exec($ch);
  19.     curl_close($ch);
  20. if(curl_errno($ch))  //catch if curl error exists and show it
  21.   echo 'Curl error: ' . curl_error($ch);
  22. else
  23.   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.
  1. $url = 'http://www.domain.com';
  2.     $data = array (
  3.         'key1' => 'value1',
  4.         'key2' => 'value2',
  5.         'key3' => 'value3'
  6.         );
  7.        
  8.         $params = '';
  9.     foreach($data as $key=>$value)
  10.                 $params .= $key.'='.$value.'&';
  11.          
  12.         $params = trim($params, '&');
  13.     $ch = curl_init();
  14.     curl_setopt($ch, CURLOPT_URL, $url); //Remote Location URL
  15.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Return data instead printing directly in Browser
  16.     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 7); //Timeout after 7 seconds
  17.     curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
  18.     curl_setopt($ch, CURLOPT_HEADER, 0);
  19.        
  20.     //We add these 2 lines to create POST request
  21.     curl_setopt($ch, CURLOPT_POST, count($data)); //number of parameters sent
  22.     curl_setopt($ch, CURLOPT_POSTFIELDS, $params); //parameters data
  23.    
  24.     $result = curl_exec($ch);
  25.     curl_close($ch);
  26.     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