Home PHP JavaScript CSS WordPress APIs .htaccess Other How-To Useful Scripts What I Recommend
Posted on by Aleksandar Gichevski ()
For those that don't know Curl is a library that allows you to connect and communicate to servers with different types of protocols. With curl at the moment of writing this post you are able to use following protocols: • http • https • ftp • gopher • telnet • dict • file • idap From PHP 4.0.2, curl library also hav support for HTTP POST, HTTP PUT, HTTPS certificates, HTTP form based upload, FTP uploading, proxies, cookies and user/password authentication.

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 You Might Like

Get a fast, free website audit


Enter your URL below to get full in-depth SEO report and tips.

‚Äč

Useful Scripts

Most Popular Posts

Recent Posts

June 14th, 2014
Website Optimization Using Gzip Compression

June 10th, 2014
Google Search AutoComplete API

May 14th, 2014
What is a Tag Cloud and How to Calculate it by Formula

March 27th, 2014
Error writing file /tmp (errcode 28) Solved!

February 15th, 2014
Fixed CSS & HTML Navigation Bar

February 9th, 2014
Benchmark Your Server (CPU, File IO, MySQL) with SysBench

February 8th, 2014
JavaScript Array Basics

January 29th, 2014
JVZoo IPN API in PHP

January 25th, 2014
Payoneer Debit Card for Freelancers to receive money online

January 24th, 2014
How HTML Color Codes are generated?

Read Latest Posts directly on Facebook

Archive