Using CURL with PHP

CURL is not widely known technology but in some cases it may come as handy for example in case you want any user to provide a file download instead of the page show or to create a page which is completely depending on some other page, thus knowing a technology is handy, who knows when it come to use.


CURL is a powerful tool which can be used with different protocols, but as title suggests I am going to describe the usage of curl with php only. However you can get detailed info about using curl here. Yet I would suggest to try the tutorial for a better understanding if you are a beginner or intermediate with curl.

There are four steps in the complete process of using curl in php.

  • 1. Initialize
  • 2. Set Options
  • 3. Execute
  • 4. Close the handle

Let us make first example to see the process, All I am going to do is to fetch the google homepage on our page.

	//set up the curl handle
	$ch = curl_init();
	
	//set curl options this is important and complete control of curl is with the options
	curl_setopt($ch,CURLOPT_URL,"www.google.co.in");
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_HEADER,0);
	
	//ececute the curl command
	$curl_response = curl_exec($ch);
	
	echo $curl_response;
	
	//finally close the handle
	curl_close($ch);

startup is real easy to understand, I have created only a handle by initializing the curl. second I am setting options, first option CURLOPT_URL is used to set the URL. Second option tells the curl that the response is to be returned as a string instead of outputting directly on the browser. And finally last option turns off the header of the response as we need our own page header instea of coming header.

So when you echo the result you will see the google homepage on the browser however the url will be the same as original.

One thing is to be noted that when you get the result. the images will not be displayed or will be broken. the reason is that, the coming page is not at the moment displayed on its original path but on a virtual path, so It can not find the original resources.

for example the url on which I called the page is demo
examsmyantra.com/demo/index.php

the resource is lying at
google.com/someresources

But the page is currently displayed at demo/index.php and when browser tries to find the resources at demo/someresources/ there is no such path hence the resources are not displayed. I hope this helps you to understand the problem. you can however try to tackle this problem by replacing the relative path in the coming result by its absolute path.

However lets drop this here and let us see another example here I will post the values in the request, current example was kind of GET request example now I will show you a post example.

//FileName: postexample.php
$postary = array(
	"a"=>1,
	"b"=>2,
	"c"=>3
);

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://localhost/work/curl/postout.php");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postary);
	
//ececute the curl command
$curl_response = curl_exec($ch);
	
echo $curl_response;
	
//finally close the handle
curl_close($ch);


FileName: postout.php

//if post is set print it
if(isset($_POST)){
	print_r($_POST);
}

when you run this example remember to replace the url with the proper url of your file. in this one all I have done is to add another option, that is CURLOPT_POST this tells curl to use post method to transmit data, and CURLOPT_POSTFIELDS takes an array of elements which are to be transmitted over the network.

For more SETOPT options you can see the reference here.

you can see basic demo here. and post demo here.