CURL has become increasingly popular over the past few years specially because of the fact that the web is becoming an active entity rather than a passive one , as it was in the yester years. CURL is a wonderful tool to communicate with other websites. CURL is also widely used in those “auto comment submit” type scripts. It makes the life of PHP developers easy. Alot of developers just consider it as a magic tool where as its nothing more than a way to format the request headers and play around with the response headers. Lets take a brief look at the insides of some of the CURL functions and what it really does. This article will be useful for situations where the curl library is not installed on the server.

$ch = curl_init();
Create the curl handle for use later.

curl_setopt($ch, CURLOPT_URL,”http://www.abc.com/hello.php”);
Tell curl the url you want to process.

curl_setopt ($ch, CURLOPT_HEADER, true);
Tell curl if it needs to return the response header as well along with the data.

curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);

Tell curl if it should oblige with the “Location:xyz.php” type response header command.

curl_setopt($ch, CURLOPT_TIMEOUT, 10);
This is the connection timeout limit.

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Tell curl if it should return the data or not.

curl_setopt($ch, CURLOPT_USERAGENT, “Mozilla/4.0 (compatible; MSIE 6.0;Windows NT 5.1)”);
Put the user agent type in the request header. This identifies what type of browser is requesting the page

curl_setopt($ch, CURLOPT_REFERER, “http://www.google.com”);
Put the referer url in the request header.Referer carries the page url where the user clicked on a link to request this page.

curl_setopt($ch, CURLOPT_COOKIEJAR, “cookies.txt”);
Provide the file name where curl will store the cookies sent by the web server in response headers.

curl_setopt($ch, CURLOPT_COOKIEFILE, “cookies.txt”);
Provide the file name where cookies are stored , and curl will send the cookies in there in the request headers.

curl_setopt($ch, CURLOPT_POSTFIELDS, array(‘username’=>’abc’ , ‘pass’=>’xyz’));
Provide the post data if any as array , curl will put it in the request headers.

curl_setopt($ch, CURLOPT_PROXY, “11.11.11.11:8080″ );
Tell curl if it should send the request via a proxy , provide the ip and port.

curl_setopt($ch, CURLOPT_PROXYUSERPWD,”user:pass”);
If the above proxy requires authentication , provise the username and password here.

$data = curl_exec($ch);
Execute curl with all the above options and put the data in the $data variable.

curl_close ($ch);
Close the connection

Now lets try to make some code which attempts to do the above with PHP.Please note that i am not trying ot create a full fledge CURL alternative here , i am just showing by example that it can be done easily with PHP , all you need to do is have some experience with headers. This is a not a perfect example script , so feel free to experiment around with it to get desired results.

You can download the below code if my visual presentation skills with wordpress dont strike you as too good :) - Download the PHP CURL Alternative Example

  1. <?
  2. $proxyip = "abc.com" ;
  3. $proxyport = "8080";
  4. $proxyuser= "proxy";
  5. $proxypass = "pass";
  6. $urltoprocess = "http://www.xyz.com/testit.php";
  7. $cookiefile = "cookies.txt"; // file must be set to be writable
  8. $referer = "http://www.google.com";
  9. $useragent = "Mozilla/4.0 (compatible; MSIE 6.0;Windows NT 5.1)";
  10. $returntransfer = true; // — show return data
  11. $showresheader = true; // –show response header
  12. $shoreqheader  = true; // –show request header
  13. $parseurl = parse_url($urltoprocess);
  14. /////////////////
  15. //— make the post data string here
  16. $poststr = "";
  17. $postdata = array(‘username’=>‘abc’ , ‘pass’=>‘xyz’);
  18. foreach($postdata as $fldname => $fldval){
  19. $poststr .= "$fldname=".urlencode($fldval)."&amp;";
  20. }
  21. /////////////////
  22. $contentlen = strlen($poststr);
  23. //– make the request header below
  24. //— check if proxy is to be used
  25. if($proxyip &amp;&amp; $proxyport){
  26. $requestheader = "POST  $urltoprocess HTTP/1.1rn";
  27. $requestheader .= "Host: $proxyiprn";
  28. /////////////////
  29. if($proxyuser &amp;&amp; $proxypass){
  30. $requestheader .= "Proxy-Authorization: Basic ".base64_encode("$proxyuser:$proxypass")."rn";
  31. }
  32. }else{
  33. $requestheader = "POST  $parseurl[path] HTTP/1.1rn";
  34. $requestheader .= "Host: $parseurl[host]rn";
  35. }
  36. //////////////////
  37. if($referer) $requestheader .= "Referer: http://www.google.comrn";
  38. if($useragent) $requestheader .= "User-Agent: $useragentrn";
  39. /////////////////
  40. //–add cookies to request header if cookies are to be used
  41. if($cookiefile){
  42. $allcookies = implode("" , file($cookiefile));
  43. if($allcookies){
  44. $requestheader .= "Cookie: $allcookiesrn";
  45. }
  46. }
  47. //////////////
  48. //–add post data to request header if any
  49. if($poststr){
  50. $requestheader .= "Content-Type: application/x-www-form-urlencodedrn";
  51. $requestheader .= "Content-Length: $contentlenrn";
  52. $requestheader .= "rn";
  53. $requestheader .= $poststr;
  54. }
  55. //////////////
  56. if($showreqheader){
  57. echo "<pre>";
  58. echo $requestheader;
  59. echo "</pre>";
  60. }
  61. /////////////////
  62. if($proxyip &amp;&amp; $proxyport){
  63. $fp = fsockopen($proxyip , $proxyport , $errno , $errstr , 10);
  64. }else{
  65. if(!$parseurl[port]){
  66. if($parseurl[scheme] == "http") $parseurl[port] = 80;
  67. if($parseurl[scheme] == "https") $parseurl[port] = 443;
  68. }
  69. $fp = fsockopen($parseurl[host] , $parseurl[port] , $errno , $errstr , 10);
  70. }
  71. /////////////////
  72. /////////////////
  73. if(!$fp){
  74. echo "$errno : $errstr <br>";
  75. }
  76. /////////////////
  77. /////////////////
  78. fputs($fp , $requestheader);
  79. while(!feof($fp)){
  80. $raw .=fgets($fp , 1024);
  81. }
  82. fclose($fp);
  83. /////////////////
  84. /////////////////
  85. $setcookies = "";
  86. if($raw){
  87. $expl = preg_split("/(rn){2,2}/", $raw, 2) ;
  88. $header = $expl[0];
  89. $data = $expl[1];
  90. /////////////////
  91. /////////////////
  92. if($cookiefile){
  93. $headlines = explode("rn" , $header);
  94. foreach($headlines as $headline){
  95. if(substr_count($headline , "Set-Cookie")){
  96. $setcookies .= trim(str_replace("Set-Cookie: " , "" , $headline))."; ";
  97. }
  98. }
  99. }
  100. }
  101. /////////////////
  102. /////////////////
  103. if($setcookies){
  104. $fp = fopen($cookiefile , "w");
  105. fwrite($fp , $setcookies);
  106. fclose($fp);
  107. }
  108. /////////////////
  109. /////////////////
  110. if(!$returntransfer){
  111. $data = "";
  112. }
  113. /////////////////
  114. /////////////////
  115. if($showresheader){
  116. $data = $header."rnrn".$data;
  117. }
  118. /////////////////
  119. /////////////////
  120. echo $data;
  121. /////////////////
  122. ?>