XVIII. CURL, Client URL Library Functions

简介

PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP's ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication.

These functions have been added in PHP 4.0.2.

需求

In order to use PHP's cURL functions you need to install the libcurl package. PHP requires that you use libcurl 7.0.2-beta or higher. In PHP 4.2.3, you will need libcurl version 7.9.0 or higher. From PHP 4.3.0, you will need a libcurl version that's 7.9.8 or higher. PHP 5.0.0 requires a libcurl version 7.10.5 or greater.

安装

To use PHP's cURL support you must also compile PHP --with-curl[=DIR] where DIR is the location of the directory containing the lib and include directories. In the "include" directory there should be a folder named "curl" which should contain the easy.h and curl.h files. There should be a file named libcurl.a located in the "lib" directory. Beginning with PHP 4.3.0 you can configure PHP to use cURL for URL streams --with-curlwrappers.

Note to Win32 Users: In order to enable this module on a Windows environment, libeay32.dll and ssleay32.dll must be present in your PATH.

You don't need libcurl.dll from the cURL site.

资源类型

This extension defines two resource types: a cURL handle and a cURL multi handle.

范例

Once you've compiled PHP with cURL support, you can begin using the cURL functions. The basic idea behind the cURL functions is that you initialize a cURL session using the curl_init(), then you can set all your options for the transfer via the curl_setopt(), then you can execute the session with the curl_exec() and then you finish off your session using the curl_close(). Here is an example that uses the cURL functions to fetch the example.com homepage into a file:

例 1. Using PHP's cURL module to fetch the example.com homepage

<?php

$ch
= curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

目录
Constants -- Curl Predefined Constants
curl_close -- Close a cURL session
curl_copy_handle -- Copy a cURL handle along with all of its preferences
curl_errno -- Return the last error number
curl_error -- Return a string containing the last error for the current session
curl_exec -- Perform a cURL session
curl_getinfo -- Get information regarding a specific transfer
curl_init -- Initialize a cURL session
curl_multi_add_handle -- Add a normal cURL handle to a cURL multi handle
curl_multi_close -- Close a set of cURL handles
curl_multi_exec -- Run the sub-connections of the current cURL handle
curl_multi_getcontent -- Return the content of a cURL handle if CURLOPT_RETURNTRANSFER is set
curl_multi_info_read -- Get information about the current transfers
curl_multi_init -- Returns a new cURL multi handle
curl_multi_remove_handle -- Remove a multi handle from a set of cURL handles
curl_multi_select -- Get all the sockets associated with the cURL extension, which can then be "selected"
curl_setopt_array -- Set multiple options for a cURL transfer
curl_setopt -- Set an option for a cURL transfer
curl_version -- Gets cURL version information

add a note add a note User Contributed Notes
mtsoft at mt-soft dot com dot ar
09-Jul-2007 09:30
This function takes a URL and returns a plain-text version of the page. It uses cURL to retrieve the page and a combination of regular expressions to strip all unwanted whitespace. This function will even strip the text from STYLE and SCRIPT tags, which are ignored by PHP functions such as strip_tags (they strip only the tags, leaving the text in the middle intact).

Regular expressions were split in 2 stages, to avoid deleting single carriage returns (also matched by \s) but still delete all blank lines and multiple linefeeds or spaces, trimming operations took place in 2 stages.

function webpage2txt($url)
{
$user_agent = Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0);

$ch = curl_init();    // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1);              // Fail on errors
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    // allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_PORT, 80);            //Set the port number
curl_setopt($ch, CURLOPT_TIMEOUT, 15); // times out after 15s

curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);

$document = curl_exec($ch);

$search = array(@<script[^>]*?>.*?</script>@si,  // Strip out javascript
@<style[^>]*?>.*?</style>@siU,    // Strip style tags properly
@<[\/\!]*?[^<>]*?>@si,            // Strip out HTML tags
@<![\s\S]*?[ \t\n\r]*>@,        // Strip multi-line comments including CDATA
/\s{2,}/,

);

$text = preg_replace($search, \n, html_entity_decode($document));

$pat[0] = /^\s+/;
$pat[2] = /\s+\$/;
$rep[0] = ;
$rep[2] = ;

$text = preg_replace($pat, $rep, trim($text));

return $text;
}

Potential uses of this function are extracting keywords from a webpage, counting words and things like that. If you find it useful, drop us a comment and let us know where you used it.
Mr.KTO
07-Jul-2007 01:59
Don't foget to curl_close($ch); Even if curl_errno($ch) != 0

Because if you don't - on Windows this will produce windows-error-report (Program terminated unexpectedly)
ashw1 - at - no spam - post - dot - cz
03-Jun-2007 11:51
In case you wonder how come, that cookies don't work under Windows, I've googled for some answers, and here is the result: Under WIN you need to input absolute path of the cookie file.

This piece of code solves it:

<?php

if ($cookies != '')
   {
   if (
substr(PHP_OS, 0, 3) == 'WIN')
       {
$cookies = str_replace('\\','/', getcwd().'/'.$cookies);}
  
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
  
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies);
   }

?>
pyroevi at yahoo dot com
30-May-2007 11:58
Earlier I posted about using php to interface with an authorize.net gateway. I accidentally made a mistake in my code.

Instead of:

curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($AuthNetConn, CURLOPT_CAINFO, "path:/ca-bundle.crt");

It should be

curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($connection, CURLOPT_CAINFO, "path:/ca-bundle.crt");

Whatever you call that first connection variable, it needs to be the same in both statements. Sorry about that.
Mr.KTO
17-May-2007 02:43
There is a bug in file uploading using multipart/form-data. (WinXP, PHP 4.4.4, libcurl/7.14.0)

curl_setopt($ch, CURLOPT_POSTFIELDS, array("userfile" => "@filename.ext");
produces error "failed creating formpost data"

Solution - use absolute path for uploaded file. As:
curl_setopt($ch, CURLOPT_POSTFIELDS, array("userfile" => '@'.realpath('filename.ext'));

p.s: Also slash before '@' don't escape it, so post values with first '@' by multipart mothod not supported.
rk6314 at gmail dot com
16-May-2007 07:25
I also think that a leading ampersand is a special escape for the cURL library to tell it that this post field is actually a path to a file that should be sent to the web server.

Not sure if that works yet, but I sure hope so.  I don't relish the thought of rolling my own multi-part submitter by hand for something I'm working on.
trucex at gmail dot com
14-May-2007 03:51
Many people still use the fopen wrappers, file_get_contents(), or even include() to open files across the internet. The issue with this is that they were not designed to do so and they are inefficient in doing so. Curl is a great alternative to these and isn't much harder to use. It even beats out the use of sockets in fairly default configurations.

I have created a script that tests all 4 methods on 20 different websites 10 times each. The results are clear, here is one set of the results:

Curl time:    43.02 seconds
FGC time:    86.48 seconds
Fopen time:    86.34 seconds
Socket time:    44.91 seconds

As you can see, there is little performance difference between file_get_contents and fopen/fread. Likewise, there is little difference between curl and sockets.

You may use the test to see the results in realtime if you'd like: http://trucex.com/curltest.php
To Frank DAVIS
09-May-2007 10:54
Post variables should be properly urlencode()'d, so, for your example,
$data = array('text' => urlencode(stripslashes("@pouetpouet")));
Frank DENIS
28-Apr-2007 09:41
There's a serious bug with CURLOPT_POSTFIELDS and multipart data :

<?php

$data
= array('text' => "@pouetpouet");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:9999');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);

?>

You can grab the output with something like "nc -l -p 9999" (Linux) or "nl -l 9999" (OpenBSD).

If the text begins with a @ (@pouetpouet), nothing is sent. If the text doesn't begin with @, the request is properly sent.

Another issue is that there's a slight delay between the header and the first block of multipart data.
killermonk at REMOVE dot killermonk dot com
30-Mar-2007 09:57
I would like to point out that in my post below "button_name" is used as the name="" attribute of the submit image.

So if you had the following code:
<input type="image" name="cmd_login" onclick="if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); " language="javascript" id="cmd_login" src="/images/cmd_login.gif" alt="Login" border="0" />

then you would use "cmd_login.x" and "cmd_login.y"
killermonk at REMOVE dot killermonk dot com
06-Mar-2007 07:23
For anyone trying to use cURL to submit to an ASP/ASPX page that uses an image as the submit button.

Make sure that you have 'button_name.x' and 'button_name.y' in the post fields. PHP names these fields 'button_name_x' and 'button_name_y', while ASP uses a dot.

Also, as noted above, be sure to include the '__VIEWSTATE' input field in your post request.
gordon_e_rouse at yahoo dot com dot au
25-Jan-2007 10:24
Using curl to take snapshots of the current page for emailing the HTML is a clever little idea. (ie: Email this page to a friend)

//to be explained below!
session_write_close();

$pageurl = "http://www.site.com/content.php?PHPSESSID=123XYZ
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_URL, $pageurl );
$html = curl_exec ( $ch );
curl_close($ch);

//then you need to fix pathing to absolute
$search = "/(src|href|background)=\"[^:,^>,^\"]*\"/i";

preg_match_all ( $search, $html, $a_matches );

//you can figure out the rest ! but thought the reg expression is useful as well

But here is the catch, you may want to make sure curl connects to the server under the same session as the browser. So naturally you pass the session cookie through the curl system either by the cookie jar system, or through the query string in the path.

This is where you will get stuck. PHP will need write access to the same session file simultaneously!! causing serious hanging issues!

This is why you should close off your session before you make curl take a page snapshot!
michael (at) networkstuff co nz
20-Jan-2007 09:05
If PHP configure fails with cURL errors, try ommiting the --with-curl=path and just make this --with-curl.
Of course it will also be optimal to make sure that the cURL library directory is listed in /etc/ld.so.conf and then run 'ldconfig'.
By default this is /usr/local/lib.
null at nahdah dot com
27-Nov-2006 09:42
I was having problems with Authorize.net and the SSL cert matching even after adding:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

What I found after a lot of stumbling was I needed to set VERIFYHOST to FALSE.  So if you are still have a problem with Authorize.NET SSL and cURL add this:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
pyroevi at yahoo dot com
22-Nov-2006 06:33
I was working on using php to interface with an authorize.net gateway, and I ran into a problem with certificates using curl to talk the https:// url.

curl_error() told me "SSL certificate problem, verify that the CA cert is OK."

I googled it and found the same "solution" over and over again: bypass verification by adding this line after curl_init():

curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, false);

This worked great, but I was required to verify, so here's what I did. Add the following lines:

curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($AuthNetConn, CURLOPT_CAINFO, "path:/ca-bundle.crt");

with "path:/ca-bundle.crt" being the path to that certificate file. You can get this file by downloading the curl package (http://curl.haxx.se/download.html) and looking for it in the lib folder.

Feel free to email me.
jon at jonroig--- dot com
25-Aug-2006 01:53
For anyone who is having trouble getting some of the advanced functionality to work in whatever version of PHP you have, I wrote a little wrapper for the command line version of curl. The function below is for Windows (hence, the curl.exe), but it works the same way under Linux or whatever.

The curl man page enumerates a ton of options you can use...

function curlPageGrabber($destinationURL, $refererURL, $postData, $autoForward = false, $autoGrabCookies = true, $curlDebug = false)
  {
  $curlString = "c:\curl\curl.exe -i -v -m 30 -L -b c:\curl\cookiejar.txt -c c:\curl\cookiejar.txt ";
  $curlString .= " -A \"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322;)\" ";
  $curlString .= " -H \"Accept-Language: en-us\" ";
  $curlString .= " -H \"Accept-Encoding: gzip, deflate\" ";
  $curlString .= " -H \"Host: affiliate-program.amazon.com\" ";
  $curlString .= " -H \"Accept: */*\" ";
  $curlString .= " --compressed ";
  //$curlString .= " --trace-ascii ";
 
  if ($refererURL != '')
   {
   $curlString .= "-e \"".$refererURL."\" ";
   }
  if ($postData != '')
   {
   $curlString.= " -d \"$postData\" ";
   }
  $curlString .= $destinationURL;
  if ($curlDebug == true)
   {
   echo "<p>curlString: $curlString</p>";
   }
 
  $temp = exec($curlString, $retval);

  $htmlData = '';
  foreach ($retval as $arrayLine)
   {
   $htmlData .= $arrayLine."\r\n";
   }
  if ($curlDebug == true)
   {
   echo "<textarea cols=80 rows=20>$htmlData</textarea>";
   }
 
  return $htmlData;
 
 
  } // end function
galvao at galvao.eti.br
07-Jul-2006 03:27
Note that on Win32 this documentation can get a little confusing.

In order to get this to work you need to:

1) Be sure that the folder where libeay32.dll and ssleay32.dll - tipically C:\\PHP - is present on the PATH variable.

2) Uncomment - remove the semi-colon - the line that says "extension=php_curl.dll" from php.ini

3) Restart the webserver (you should already know this one, but...)

It took me some time to realize this, since this page doesn't mention the need to uncomment that php.ini's line.
david at neocodesoftware dot com
31-May-2006 12:42
This may be obvious to everybody *except* me, but if you want to use curl to connect via ftp rather than http, then you just need to use "ftp://" in the url specification (I was looking for an use_ftp flag or something). 

Use the CURLOPT_USERPWD to login to the ftp site.
Sean Huber
05-May-2006 11:01
<?php
/*
Sean Huber CURL library

This library is a basic implementation of CURL capabilities.
It works in most modern versions of IE and FF.

==================================== USAGE ====================================
It exports the CURL object globally, so set a callback with setCallback($func).
(Use setCallback(array('class_name', 'func_name')) to set a callback as a func
that lies within a different class)
Then use one of the CURL request methods:

get($url);
post($url, $vars); vars is a urlencoded string in query string format.

Your callback function will then be called with 1 argument, the response text.
If a callback is not defined, your request will return the response text.
*/

class CURL {
   var
$callback = false;

function
setCallback($func_name) {
  
$this->callback = $func_name;
}

function
doRequest($method, $url, $vars) {
  
$ch = curl_init();
  
curl_setopt($ch, CURLOPT_URL, $url);
  
curl_setopt($ch, CURLOPT_HEADER, 1);
  
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
  
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
   if (
$method == 'POST') {
      
curl_setopt($ch, CURLOPT_POST, 1);
      
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
   }
  
$data = curl_exec($ch);
  
curl_close($ch);
   if (
$data) {
       if (
$this->callback)
       {
          
$callback = $this->callback;
          
$this->callback = false;
           return
call_user_func($callback, $data);
       } else {
           return
$data;
       }
   } else {
       return
curl_error($ch);
   }
}

function
get($url) {
   return
$this->doRequest('GET', $url, 'NULL');
}

function
post($url, $vars) {
   return
$this->doRequest('POST', $url, $vars);
}
}
?>
tom at luigjes dot org
29-Mar-2006 10:48
When you try to login to a page using cURL, you could experience problems when you're not providing the right URL.

For instance, if you use the front page to login (http://awebsite.com/) where to form is located, you will receive a http 405 error, instead view the source and see what the form's action is, it might just be: http://awebsite.com/processlogin.php

Took me hours to find this out, hope it can save someone else's time.
richardkmiller AT gmail
03-Mar-2006 12:21
Beware of any extra spaces in the URL.  A trailing space in the URL caused my script to fail with the message "empty reply from server".
heyrocker at yahoo dot com
23-Feb-2006 09:57
The examples below for HTTP file upload work great, but I wanted to be able to post multiple files through HTTP upload using HTML arrays as specified in example 38.3 at

http://us3.php.net/features.file-upload

In this case, you need to set the arrays AND keys in the $post_data, it will not work with just the array names. The following example shows how this works:

<?php

   $post_data
= array();
  
  
$post_data['pictures[0]'] = "@cat.jpg";
  
$post_data['pictures[1]'] = "@dog.jpg";
  

  
$ch = curl_init();
  
curl_setopt($ch, CURLOPT_URL, "http://my.domain.com/my_url.php" );
  
curl_setopt($ch, CURLOPT_POST, 1 );
  
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  
$postResult = curl_exec($ch);

   if (
curl_errno($ch)) {
       print
curl_error($ch);
   }
  
curl_close($ch);
   print
"$postResult";
?>
stevehartken at hotmail dot com
22-Feb-2006 06:40
I had the following experience when harvesting urls with a get variable from a page using cUrl. HTML pages will output ampersands as &amp when the page is read by the curl function.

If you code a script to find all hyperlinks, it will use &amp instead of &, especially using a regular expression search.

It is hard to detect because when you output the url to the browser it renders the html. To fix, add a line to replace the &amp with &.

function processURL($url){
       $url=str_replace('&amp;','&',$url);
       $ch=curl_init();
       curl_setopt($ch, CURLOPT_URL, $url);
       curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
       $xml = curl_exec ($ch);
       curl_close ($ch);
       echo $xml;
   }
}
petelu $ post dot sk
17-Feb-2006 12:19
load https:// or  http://ex.com/exam.php 
with  POST  data (name=alex&year=18) and apply COOKIEs

$sessions = curl_init();
curl_setopt($sessions,CURLOPT_URL,'http://ex.com/exam.php');
curl_setopt($sessions, CURLOPT_POST, 1);
curl_setopt($sessions,CURLOPT_POSTFIELDS,'name=alex&year=18');
curl_setopt($sessions,CURLOPT_COOKIEJAR,
dirname(__FILE__).'/cookie.txt');
curl_setopt($sessions,CURLOPT_FOLLOWLOCATION,0);
curl_setopt($sessions, CURLOPT_HEADER , 1);
curl_setopt($sessions, CURLOPT_RETURNTRANSFER,1);
$my_load_page = curl_exec($this->sessions);
amandato (at) gmail (period) com
06-Feb-2006 09:53
To shed some light on some of the numbers trucex at gmail has in his tests, some of the web pages (Yahoo and GoDaddy) downloaded at exceptionally faster speeds than with file_get_contents().  This is simply because CURL accepts (by default) compression protocols such as deflate and gzip.

Most likely GoDaddy.com may be using compression for specific HTTP clients including CURL.  I know for a fact that Yahoo.com and Google.com use gzip compression.  You can test servers for gzip compression here:  http://www.whatsmyip.org/mod_gzip_test/

For the sake of performance, I would recommend the use of CURL.  if performance was not an issue and I wanted to keep my code simple and portable (for PHP servers without the cURL extension) I would use file_get_contents().
luca dot manzo at bbsitalia dot com
02-Feb-2006 11:55
If you're getting trouble with cookie handling in curl:

- curl manages tranparently cookies in a single curl session
- the option
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName");

makes curl to store the cookies in a file at the and of the curl session

- the option
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookieFileName");

makes curl to use the given file as source for the cookies to send to the server.

so to handle correctly cookies between different curl session, the you have to do something like this:

       $ch = curl_init();
       curl_setopt ($ch, CURLOPT_URL, $url);
       curl_setopt ($ch, CURLOPT_COOKIEJAR, COOKIE_FILE_PATH);
       curl_setopt ($ch, CURLOPT_COOKIEFILE, COOKIE_FILE_PATH);

       curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
       $result = curl_exec ($ch);
       curl_close($ch);
       return $result;

in particular this is NECESSARY if you are using PEAR_SOAP libraries to build a webservice client over https and the remote server need to establish a session cookie. in fact each soap message is sent using a different curl session!!

I hope this can help someone
Luca
administrator at gesoft dot org
24-Dec-2005 04:39
In replay to trucexs post,

You are right in case if we will talk about simple script, like you mention. Now just imagine situation when you will need to create some kind of web based proxy (example gesoft.org)? Can it be done by using simple functions like file_get_contents? The answer will be positive, but at the same time dont forget the time which will be spend to do the same job. Is it worth to spend more time?

Now most important thing in which I will never agree, is it possible to execute more than 1000 threads at the same? Yes and here is an example of it. proxy-list.org. 1000 proxies are rechecked every 10 min. The difference between first proxy testing and last one could be easily calculated and it will be equal something about 46 seconds. It should be mentioned that this testing script not only executed and stopped but it has 30 seconds timeout!!! That means that script can not finish its work 30 seconds earlier.

According my stats, if just simple web page will be opened by curl, there is possibility to execute up to 10k threads at the same time.

One more important parameter is which amount of possible simultaneously threads can be executed depends directly proportionally on hosting quality! Such as, busyness, CPU, RAM, at list internet channel quality and speed.

Regards and good luck in you work

Vitali Simsive
trucex at gmail
19-Dec-2005 03:10
I've seen a few people claim things like curl executing 2000 requests per minute (I didn't believe it, but see godaddy.com in the results below). I've also gotten into 5-6 arguments about the speed of cURL vs. other alternatives.

To stop the argument once and for all, I've created a script that will test cURL retreiving a page 50 times and file_get_contents 50 times, on a variety of different URL's.

For simplicity, I have only retreived the page from the remote site and then went on to the next request. All URL's are using the HTTP protocal; FTP is not tested.

Here are the results of the script on one execution:

Calculating 50 queries to http://www.flickr.com/.
cURL took 9.550734 seconds.
file_get_contents() took 10.878360 seconds.

Calculating 50 queries to http://www.yahoo.com/.
cURL took 4.729566 seconds.
file_get_contents() took 10.443786 seconds.

Calculating 50 queries to http://www.ebay.com/.
cURL took 46.348250 seconds.
file_get_contents() took 52.685604 seconds.

Calculating 50 queries to http://www.godaddy.com/.
cURL took 1.505460 seconds.
file_get_contents() took 37.154304 seconds.

Calculating 50 queries to http://www.php.net/.
cURL took 13.136836 seconds.
file_get_contents() took 17.981879 seconds.

You may test this yourself while the script is up as well. Make a note that the page will take a few minutes to load completely (add all of the result times above and then some).

http://monitor.trucex.com/curltest.php
administrator at gesoft dot org
08-Dec-2005 05:08
Hello everybody,
The solution how to use proxy in correct way with curl was founded. Here is the part of code which was successfully used for checking proxy server and can be used by anybody as an example. During the research one very useful parameter of curl was discovered: CURLOPT_TIMEOUT. If script is using curl object with proxy parameter it is very important to set this parameter. There is possibility that proxy will not replay (die or big latency) until PHP scripts timeout exceed.

   $cUrl = curl_init();
   curl_setopt($cUrl, CURLOPT_URL, $requestUrl);
   curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($cUrl, CURLOPT_TIMEOUT, 'timeout_in_seconds');
   curl_setopt($cUrl, CURLOPT_PROXY, 'proxy_ip:proxy_port');
   $pageContent = trim(curl_exec($cUrl));
   curl_close($cUrl);

Hope this post will be useful
Sincerely yours
Vitali Simsive
P.S. The example of working script can be seen here http://proxy-list.org/admin/checkproxy.php?proxy=ip:port&timeout=sec
If somebody is interesting to obtain personal copy of checkproxy.php please mail me.
abbyz at online dot ie
03-Dec-2005 12:27
To setup http proxy for connection use CURLPROXY_HTTP.

I found it is something problematic to configure connection based on CURLOPT_PROXY.

curl_setopt ( resource ch, CURLPROXY_HTTP, 'http://host:port');
administrator at gesoft dot org
11-Nov-2005 11:49
To use proxy in cUrl, not only CURLOPT_PROXY parameter should be set but CURLOPT_HTTPPROXYTUNNEL (to 1 or true) and CURLOPT_PROXYUSERPWD (if proxy requires authorization) as it was written in the cUrls official documentation.
Working code should be something like this:

$cUrl = curl_init();
curl_setopt($cUrl, CURLOPT_URL, google.com);
curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cUrl, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($this->cUrl, CURLOPT_PROXY, 'proxy_name_or_ip:proxy_prot');
curl_setopt($this->cUrl, CURLOPT_PROXYUSERPWD, 'username:password');
$PageContent = curl_exec($cUrl);
curl_close($cUrl);

To be honest this code does not work on my server. Parameters different variation had tried but in vain, the code still did not work. Finally personal proxy server was bought and checking started again, the result was negative again.

Before bringing up this question here, it was asked on many different PHP forums but in vain, nobody had the answer. There is only one suspicion: there is possibility that there is something wrong with hosting where this code was tested.

To find the right answer the code should be tested in different hosting.
admin at sellchain dot com
17-Oct-2005 02:43
TIP: If you are using fopen and fread to read HTTP or FTP or Remote Files, and experiencing some performance issues such as stalling, slowing down and otherwise, then it's time you learned a thing called cURL.

Performance Comparison:

10 per minute for fopen/fread for 100 HTTP files
2000 per minute for cURL for 2000 HTTP files

cURL should be used for opening HTTP and FTP files, it is EXTREMELY reliable, even when it comes to performance.

I noticed when using too many scripts at the same time to download the data from the site I was harvesting from, fopen and fread would go into deadlock. When using cURL i can open 50 windows, running 10 URL's from each window, and getting the best performance possible.

Just a Tip :)

Also here is some quick code that I use in my scripts now, of course credits go to other PHP'ers who have posted on this page, so I take zero credit.

TO POST TO A PAGE USING values and fields:
<?
$url
="http://anything";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "fieldname=fieldvalue&fieldname=fieldvalue&");
#curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);
$content = curl_exec ($ch); # This returns HTML
curl_close ($ch);
?>

TO GET A PAGE USING A URL ONLY:
<?
$url
="http://anything";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);
$xml = curl_exec ($ch);
curl_close ($ch);

?>
expert at yahoo dot com
08-Oct-2005 11:26
For more features using JavaScript XML HTTP Request Object components (AJAX), visit this page : http://expert.no-ip.org/?ajax

example.php
===========

<?php

require_once 'Ajax.php';
 
// add headers
$addheader[] = array("TEST1", "test one");
$addheader[] = array("TEST2", "test two");
 
// javascript code by http status
$cmd[] = array("alert({ajax})", 200);
$cmd[] = array("alert('Error 404 - File not found !')", 404);
$cmd[] = array("alert('HTTP Status '+{stat}+' !')", "DEFSTAT");
 
$obj = new Ajax;
// get test.php body source using method GET
$obj->request("test.php");
// set additional header information
$obj->headers($addheader);
 
?>
<html>
<head>
<?=$obj->head();?>
</head>
<body>
<?=$obj->get($cmd);?>
Example.php
</body>
</html>

test.php
========

<?php

// print header information HTTP_TEST1 & HTTP_TEST2
echo "HTTP_TEST1: ".$_SERVER['HTTP_TEST1']."\n".
    
"HTTP_TEST2: ".$_SERVER['HTTP_TEST2'];
 
?>
ROXORT at TGNOOB dot FR
21-Sep-2005 02:09
/*
  Here is a script that is usefull to :
  - login to a POST form,
  - store a session cookie,
  - download a file once logged in.
*/

// INIT CURL
$ch = curl_init();

// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL, 'http://www.external-site.com/Members/Login.php');

// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);

// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'fieldname1=fieldvalue1&fieldname2=fieldvalue2');

// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

# Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
# not to print out the results of its query.
# Instead, it will return the results as a string return value
# from curl_exec() instead of the usual true/false.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

// EXECUTE 1st REQUEST (FORM LOGIN)
$store = curl_exec ($ch);

// SET FILE TO DOWNLOAD
curl_setopt($ch, CURLOPT_URL, 'http://www.external-site.com/Members/Downloads/AnnualReport.pdf');

// EXECUTE 2nd REQUEST (FILE DOWNLOAD)
$content = curl_exec ($ch);

// CLOSE CURL
curl_close ($ch);

/*
  At this point you can do do whatever you want
  with the downloaded file stored in $content :
  display it, save it as file, and so on.
*/
webmaster at oscontract dot com
31-Aug-2005 05:26
Just an addition to ksavage's script. Iam using HSBC API method of integration.

We may not need those array of xml tags.
We may need to check and fetch values of few strings.

<?

print_r
($vals);  // print all the arrays.

print_r($vals[29]); // print only the selected array.

//prints something like below.

//Array ( [tag] => ProcReturnMsg [type] => complete [level] => 6 [attributes] => Array ( [DataType] => String ) [value] => Approved )

$val1 = $vals[29];

print_r ($val1[tag]);  // ProcReturnMsg

print_r ($val1[value]); //Approved

?>
imranlink at hotmail dot com
25-Aug-2005 01:05
If you need Specific Help in CURL PHP Please visit the site http://curl.phptrack.com

CURL PHP Forum (http://curl.phptrack.com/forum/index.php) help a lot for beginners students and PHP professional to write Commercial Applications by using PHP with cURL support.
alex at javafaq dot nu
04-Aug-2005 12:00
Hello!

CURL function make security hole at your site....

Just read that PHP cURL functions bypass open_basedir

protection, so users can navigate through

filesystem.
For example, setting "open_basedir" in php.ini to

"/var/www/html" anybody can retrieve "/etc/parla"

using cURL functions.

== Proof of concept (curl.php)

<?php

$ch
= curl_init("file:///etc/parla");

$file=curl_exec($ch);

echo
$file

?>

More details here:
http://www.securityfocus.com/archive/1/379657/2004-10-26/2004-11-01/0
eric at harmonylinemusic dot com
24-Jun-2005 09:19
After trying both ksavage and mmm's attempts to send HTTP POST's with full xml in them, I was finally successful after using an obscure content-type (multipart/mixed) with a boundary declaration. Incidentally, CURLOPT_CUSTOMREQUEST only accepts method names like 'POST' or 'PUT', not entire headers. You need to use CURLOPT_HTTPHEADER.

<?php
$request
= "<xml>...</xml>";

      
$header[] = "Host: $host";
      
$header[] = "MIME-Version: 1.0";
      
$header[] = "Content-type: multipart/mixed; boundary=----doc";
      
$header[] = "Accept: text/xml";
      
$header[] = "Content-length: ".strlen($request);
      
$header[] = "Cache-Control: no-cache";
      
$header[] = "Connection: close \r\n";
      
$header[] = $request;
    
      
$ch = curl_init();
      
curl_setopt($ch, CURLOPT_URL,$url);
      
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST');
      
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
?>

On the other side, the server grabs the post data and strips the boundaries.

<?php
         $somecontent
= $HTTP_RAW_POST_DATA;
        
$somecontent = trim(str_replace('----doc','',$somecontent));
        
         echo
$somecontent;
        
//returns <xml>...</xml>
?>

Back at the client, the server's output is taken by curl_exec() and stored in $data

<?php
  $data
= curl_exec($ch);       
       if (
curl_errno($ch)) {
           print
curl_error($ch);
       } else {
          
curl_close($ch);
           echo
$data;
          
//returns <xml>...</xml>
      
}
?>

Compared to SOAP or REST, this is really an enormous pain, plus the server still has to parse the xml to be able to use it. But sometimes it's unavoidable. Enjoy.

Eric
michaeledwards.com
01-Jun-2005 09:57
Problems can occur if you mix CURLOPT_URL with a 'Host:' header in CURLOPT_HEADERS on redirects because cURL will combine the host you explicitly stated in the 'Host:' header with the host from the Location: header of the redirect response.

In short, don't do this:

$host = "www.dontusehostheaders.com";
$url = "http://$host/";

$headers = array("Host: $host");

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

Do this instead:

$host = "www.dontusehostheaders.com";
$url = "http://$host/";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
samlowry at e-baka dot net
14-May-2005 01:09
About CURLOPT_ENCODING:
added in curl in 7.10 - Oct 1 2002
In 7.10.5 - May 19 2003 syntax was chnaged:
"setting CURLOPT_ENCODING to "" automaticly enables all supported encodings"
ksavage at achaean dot com
28-Mar-2005 09:25
Had one heck of a time getting curl to send my XML request.  Tried a lot of different things, FINALLY ended up with this. 

Im making a curl request to paymentech / Orbital 's payment gateway.  Essentially, I took the same header i made for the fsockopen() request, and used it for this curl request. (Hostinc company didnt allow fsockopen().

You'll note that this specific gateway requires a custom content type header.  Thats what gave me the most trouble.  When using the CURLOPT_HTTPHEADER, and CURLOPT_POSTFIELDS together, it doesnt matter, it sends the default post header because of CURLOPT_POSTFIELDS.

<?php

$url
= "https://your.gateway.url";
$page = "/proccessing_page.cgi";

$post_string = "<YourXML>All XML stuff Here</YourXML>";

      
$header  = "POST ".$page." HTTP/1.0 \r\n";
      
$header .= "MIME-Version: 1.0 \r\n";
      
$header .= "Content-type: application/PTI26 \r\n";
      
$header .= "Content-length: ".strlen($post_string)." \r\n";
      
$header .= "Content-transfer-encoding: text \r\n";
      
$header .= "Request-number: 1 \r\n";
      
$header .= "Document-type: Request \r\n";
      
$header .= "Interface-Version: Test 1.4 \r\n";
      
$header .= "Connection: close \r\n\r\n";
      
$header .= $post_string;
      
      
$ch = curl_init();
      
curl_setopt($ch, CURLOPT_URL,$url);
      
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);

      
$data = curl_exec($ch);        if (curl_errno($ch)) {
           print
curl_error($ch);
       } else {
          
curl_close($ch);
       }

// use XML Parser on $data, and your set!

      
$xml_parser = xml_parser_create();
      
xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,0);
      
xml_parser_set_option($xml_parser,XML_OPTION_SKIP_WHITE,1);
      
xml_parse_into_struct($xml_parser, $data, $vals, $index);
      
xml_parser_free($xml_parser);

// $vals = array of XML tags.  Go get em!

?>

Worked like a dream for me.  Enjoy!
lukas at MORESPAM dot jhu dot edu
05-Mar-2005 03:29
Here's a function that I needed and couldn't use CURL... it helps you if you want to send the POST data (instead of GET) from one form to 2 or more other PHP scripts.  Trust me... it's a life saver!!

     function HTTP_Post($URL,$data, $referrer="") {

       // parsing the given URL
       $URL_Info=parse_url($URL);

       // Building referrer
       if($referrer=="") // if not given use this script as referrer
         $referrer=$_SERVER["SCRIPT_URI"];

       // making string from $data
       foreach($data as $key=>$value)
         $values[]="$key=".urlencode($value);
       $data_string=implode("&",$values);

       // Find out which port is needed - if not given use standard (=80)
       if(!isset($URL_Info["port"]))
         $URL_Info["port"]=80;

       // building POST-request:
       $request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
       $request.="Host: ".$URL_Info["host"]."\n";
       $request.="Referer: $referer\n";
       $request.="Content-type: application/x-www-form-urlencoded\n";
       $request.="Content-length: ".strlen($data_string)."\n";
       $request.="Connection: close\n";
       $request.="\n";
       $request.=$data_string."\n";

       $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
       fputs($fp, $request);
       while(!feof($fp)) {
           $result .= fgets($fp, 128);
       }
       fclose($fp);

       return $result;
     }

     $output1=HTTP_Post("http://www.server1.com/script1.php",$_POST);
     $output2=HTTP_Post("http://www.server2.com/script2.php",$_POST);
christian at webaxis dot com dot au
04-Mar-2005 02:38
I use CURL to get history of e-gold payment.

function get_history($egold, $pass, $batch, $counter, $s_date,
$s_month, $s_year, $e_date, $e_month, $e_year, $type){

   $defined_vars = get_defined_vars();

   $_url = 'https://www.e-gold.com/acct/historycsv.asp';

   $params =    "AccountID=$egold&PassPhrase=$pass&" .
       "startmonth=".$s_month."&startday=".
$s_date."&startyear=".$s_year."&" .
       "endmonth=".$e_month."&endday=".
$e_date."&endyear=".$e_year."&";

if($type == "0"){
   $params .=    "paymentsmade=1&";
}
if($type == "1"){
   $params .=    "paymentsreceived=1&";
}

   if(!empty($batch)){
   $params .=    "batchfilter=$batch&";
   }

   if(!empty($counter)){
   $params .=    "counterfilter=$counter&";
   }

   $params .=    "metalfilter=1&";

   $ch = curl_init();

   curl_setopt($ch, CURLOPT_POST,1);
   curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
   curl_setopt($ch, CURLOPT_URL,$_url);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
   curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
   $content = curl_exec ($ch);

   curl_close ($ch);

   return $content;

}
Sebastian [Jegeras {snabel-a} gmail.com]
27-Jan-2005 08:51
I did this script to login to a community X number of times

<?php

$url
= [the script that handle logins];

if (empty(
$_GET[u]) && empty($_GET[p]) && empty($_GET[x])){
       echo
"<FORM NAME=make METHOD=get ACTION=".$_SERVER[PHP_SELF].">
   "
.$url."?u=<INPUT TYPE=text NAME=u value=nickname>
   &p=<INPUT TYPE=text NAME=p value=password>
   &x=<INPUT TYPE=text NAME=x value=times>
   <input type=submit>"
;
}else{   
   if(!
intval($_GET[x])) $_GET[x] = 10;
  
$time=time();
  
$params = "username=".$_GET[u]."&userpass=".urldecode($_GET[p]);
  
$count = login($url,$params);
   echo
$count."/".$_GET[x]." logins tog ".(time() - $time)." sekunder";
}

//
function login($url,$params){   
  
$user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
  
  
$ch = curl_init();
  
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  
curl_setopt ($ch, CURLOPT_HEADER, 0);
  
curl_setopt($ch, CURLOPT_POST,1);
  
curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
  
curl_setopt($ch, CURLOPT_URL,$url);
  
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  
  
$cnt=0;
   for(
$i=0;$i<($_GET[x]);$i++){
       if(
curl_exec ($ch))$cnt++;
   }
  
curl_close ($ch);
   return
$cnt;
}
?>
php dot net at lunchclub dot com
12-Jan-2005 08:34
Additional PHP curl documenation is at:
http://curl.haxx.se/libcurl/php/
josh,endquote,com
24-Dec-2004 04:58
If you're trying to post forms to an ASP.NET server, you will need to include the value of the __VIEWSTATE hidden field in your post. You can get it like this:

$html = curl_exec($ch);
$matches = array();
preg_match('/<input type="hidden" name="__VIEWSTATE" value="([^"]*?)" \/>/', $html, $matches);
$viewstate = $matches[1];
$viewstate = urlencode($viewstate);

That last urlencode() is the bit that drove me crazy for a while.
php at cobalthorizons dot com
14-Nov-2004 06:44
btw, CURLOPT_HTTPHEADER expects an array:

$myHeader = array(
   "MIME-Version: 1.0",
   "Content-type: text/html; charset=iso-8859-1",
   "Content-transfer-encoding: text",
);
  
curl_setopt($ch, CURLOPT_HTTPHEADER, $myHeader);

(hope this saves someone several hours of...)
mikeb[at]xamo[dot]com
14-Oct-2004 08:11
A note of warning for PHP 5 users: if you try to fetch the CURLINFO_CONTENT_TYPE using curl_getinfo when there is a connect error, you will core dump PHP. I have informed the Curl team about this, so it will hopefully be fixed soon. Just make sure you check for an error before you look for this data.
alidrus at langkah dot com
14-Sep-2004 09:31
In recent versions of php, CURLOPT_MUTE has (probably) been deprecated. Any attempt of using curl_setopt() to set CURLOPT_MUTE will give you a warning like this:

PHP Notice:  Use of undefined constant CURLOPT_MUTE - assumed 'CURLOPT_MUTE' in ....

If you wish tu silence the curl output, use the following instead:

   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

And then,

   $curl_output=curl_exec($ch);

The output of the curl operation will be stored as a string in $curl_output while the operation remains totally silent.
gordon_e_rouse at yahoo dot com dot au
09-Sep-2004 03:08
Multipart form uploads (ie simulating the upload of files from a browser form) is surprisingly easy, yet hard to find information on.... hope this changes that.

$postData = array();

//simulates <input type="file" name="file_name">
$postData[ 'file_name' ] = "@test.txt";
$postData[ 'submit' ] = "UPLOAD";
      
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url );       
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1 );

//seems no need to tell it enctype='multipart/data' it already knows
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData );

$response = curl_exec( $ch );

//where test.txt is a file in the same directory!
rich @ richud dot com
11-Jun-2004 02:12
If you have upgraded to using thread safe PHP (with apache 2 MPM=worker) note that
CURLOPT_COOKIEJAR / CURLOPT_COOKIEFILE both need an absolute path set for the cookie file location and no longer take a relative path.
(As before, also remember to have set correct permissions to allow a writeable cookie file/dir by apache)
[php 4.3.7/apache v2.0.49]
simon [at] vhostdirect [dot] co [dot] uk
04-Mar-2004 12:53
It took me quite some to to figure out how to get Curl (with SSL), OpenSSL and PHP to play nicely together.

After reinstalling MS-VC7 and compiling OpenSSL to finally realise this was'nt nesscary.

If your like me and like *Nix systems more than Windows then you'll most probly have similar problems.

I came across this, on a simple google with the right keywords.

http://www.tonyspencer.com/journal/00000037.htm

I read thru that and found my mistake.

Its just a small list of notes, I found them to be the best I've found on the subject and the most simplist.

Dont forget to add a simple line like this into your scripts to get them working on Win32.

<?php
if($WINDIR) curl_setopt($curl, CURLOPT_CAINFO, "c:\\windows\\ca-bundle.crt");
?>

Last note: ca-bundle.crt file is located in the Curl download. I stored mine in the windows directory and apache/php can access it fine.

All the best and I hope this helps.

Simon Lightfoot
vHost Direct Limited
hraefn at hraefn dot net
03-Feb-2004 09:15
You can request (and have deflated for you) compressed http (from mod_gzip or ob_gzhandler, for instance) by using the following:

curl_setopt($ch,CURLOPT_ENCODING , "gzip");

This seems to work fine with the latest php (4.3.4) curl (7.11.0) and zlib (1.1.4) under linux.  This is much more elegant than forcing the Accept-Encode header and using gzdeflate on an edited result.
mmm at turkmenweb dot com
15-Dec-2003 12:49
I know that many people suffer from cURL when trying to send XML data. I solved this problem and want to share it with you:

I was trying to send SMS messages with a SMS service provider. They gave me the following ASP code, where you send data as XML and receive a response in XML as well:

url = "http://...someaddress/somefile.asp";
xmldata ="<XMLtag1>many other tags, data here</XMLtag1>";
Set ob = Server.CreateObject("Msxml2.XMLHTTP")
ob.Open "POST",url,false
ob.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
ob.Send xmldata
...then recieve code was here...

This is done in PHP using cURL library in this way:

<?php
$XPost
= "<XMLcontent>sameas above</XMLcontent>"
$url = "..same URL as above..";
$ch = curl_init();    // initialize curl handle
curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 4); // times out after 4s
curl_setopt($ch, CURLOPT_POSTFIELDS, $XPost); // add POST fields
$result = curl_exec($ch); // run the whole process
echo $result; //contains response from server
?>

Some say that you should use CURLOPT_CUSTOMREQUEST, CURLOPT_HTTPHEADER and etc. to perform this task , as you can see you don't need those at all.

Hope it will help those who tries to simulate Msxml2.XMLHTTP or simply try sending XML data over PHP.

Regards,
Muhammed Mamedov
rodneywa at yahoo dot com
24-Oct-2003 08:47
Here's how I was able to post arrays using Curl:

$array_of_vars[]="var1";
$array_of_vars[]="var2";
$array_of_vars[]="var3";
$array_of_vars[]="var4";

$submit_url = "http://www.example.com/reads_post.php";
$formvars["feild1"] = "feild1";

for ($i=0;$i<sizeof($array_of_vars);$i++)
   $formvars["array_of_vars[$i]"] = $dna[$i];

  
// init curl handle
$ch = curl_init($submit_url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $formvars);

// perform post
$rr=curl_exec($ch);
echo $rr;
curl_close($ch);
da at canaan dot co dot il
08-Oct-2003 02:42
i had problems with receiving all of the data that was replied to me about a transaction, atlast after way too much time spent i found out that all i missed was:
curl_setopt($ch, CURLOPT_HEADER, 1);

hope it helped anyone
alwong at 123infosys dot com
21-Sep-2003 02:35
The following scripts show how to post form to a web server via https.  I modified rodrigo posting because it did not work for me on IIS5.
<?php
   $url
= 'https://www.yourserver.com/yourrequest;
   $params = "name=your_name&email=youremail@mail.com";
   $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_POST,1);
   curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
   curl_setopt($ch, CURLOPT_URL,$url);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
   curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  // this line makes it work under https

   $result=curl_exec ($ch);
   curl_close ($ch);

   echo("Results: <br>".$result);
?>
rodrigo dot nakahodo at dhweb dot com dot br
18-Sep-2003 07:57
The script shows how to get a simple response of a SSL Server. Enjoy it!

<?php

   $defined_vars
= get_defined_vars();

  
$_url = 'https://www.example.com.br';
  
$_VAR001 = 'nono';
  
$_VAR002 = 'nonono';

  
$params = "VAR001=$_VAR001&VAR002=$_VAR002&";

  
$ch = curl_init();
  
curl_setopt($ch, CURLOPT_POST,1);
  
curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
  
curl_setopt($ch, CURLOPT_URL,$_url);
  
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST2);
  
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
  
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  
$result=curl_exec ($ch);
  
curl_close ($ch);

   echo(
"Output: ".$result);

?>
helvecio_oliveira at yahoo dot com dot br
21-Aug-2003 04:12
CURL install steps in Mandrake 9.1:
==========================================================
cp -r /usr/src/php-devel/extensions/curl /tmp/curl
cd /tmp/curl
phpize
./configure
make install
echo "extension = curl.so" > /etc/php/90_curl.ini

Restart apache web server.
==========================================================
curl.so is in:
/usr/lib/php/extensions/

look in phpinfo, the string:
/etc/php/90_curl.ini

Needs these  tools:
autoconf
automake
libtool
m4
php430-devel-430-11mdk.rpm

Some docs:
/usr/share/doc/php430-devel-430/SELF-CONTAINED-EXTENSIONS
eric at imap dot ch
07-Jul-2003 04:38
I managed to use curl to retrieve information from severs on ports other than 80 or 443 (for https) on some installations but not on all.
If you get an "CURLE_COULDNT_CONNECT /* 7 */" error, try adding the port : (for example)
curl_setopt($ch, CURLOPT_PORT, $_SERVER['SERVER_PORT']);
php at mechintosh dot com
03-Jul-2003 03:51
IMHO the example code is somewhat confusing; the fopen() and fclose() are not needed at all, AFAICT.

By default the resulting data/page of the external server is just printed out. To assign the result of the curl session to a variable, one can use
- output buffering (see ob_start(), ob_get_contents())
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
With the latter, the result of curl_exec is the actual data instead of a boolean.
sorin
09-Apr-2003 08:44
function curl_string ($url,$user_agent,$proxy){

       $ch = curl_init();
       curl_setopt ($ch, CURLOPT_PROXY, $proxy);
       curl_setopt ($ch, CURLOPT_URL, $url);
       curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
       curl_setopt ($ch, CURLOPT_COOKIEJAR, "c:\cookie.txt");
       curl_setopt ($ch, CURLOPT_HEADER, 1);
       curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
       curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
       $result = curl_exec ($ch);
       curl_close($ch);
       return $result;
  
}

$url_page = "http://www.google.com/";
$user_agent = "Mozilla/4.0";
$proxy = "http://208.25.243.167:8080";
$string = curl_string($url_page,$user_agent,$proxy);
echo $string;

this could hep someone
Sorin
thomas at NOSPAM dot gutschke dot com
07-Apr-2003 12:00
Here's a little code snippet if you need the last-modified-time for a remote file as filemtime() does not work on those. :)

$modified = "";
function read_header($ch, $header)
{
   global $modified;
   $length = strlen($header);
   if(strstr($header, "Last-Modified:"))
   {
       $modified = substr($header, 15);
   }
   return $length;
}

function last_mod($remote_file)
{
   global $modified;
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $remote_file);
   curl_setopt($ch, CURLOPT_HEADER, 1);
   curl_setopt($ch, CURLOPT_NOBODY, 1);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');

   $headers = curl_exec ($ch);
   curl_close ($ch);
   return $modified;
}

use something like
   echo strtotime(last_mod("http://www.php.net/"));
to get the time as an Unix timestamp or
   echo last_mod("http://www.php.net/");
to get the gmdate()-like time.
dweingart at pobox dot com
02-Apr-2003 11:08
If you want to Curl to follow redirects and you would also like Curl to echo back any cookies that are set in the process, use this:

curl_setopt($ch, CURLOPT_COOKIEJAR, '-');

'-' means stdout

-dw
diana dot castillo at nvtechnologies dot com
25-Mar-2003 06:30
if you need to install curl on a windows system, this has the best explanation:
http://zend.com/zend/tut/tutorial-thome3.php
bharris at spro dot net
26-Feb-2003 12:58
For Win2000: To get the 4.3.1 curl dll to work with https you now need to download the latest win32 curl library from http://curl.haxx.se and snag the ca-bundle.crt file from the lib directory.  Place this somewhere handy on your webserver.

Then in your PHP script, add the following setopt line to the rest of your curl_setopt commands:

curl_setopt($ch, CURLOPT_CAFILE, 'C:\pathto\ca-bundle.crt')

This worked for me and allowed me to discontinue using the CURLOPT_SSL_VERIFYPEER set to zero hack.
retro at nospam dot enx dot org
12-Jan-2003 01:42
When using cURL to do posts, make sure you actually specify the CURLOPT_POSTFIELDS option. Not doing so seems to make PHP core (and apache as well if you're running it through there).
asi at neo dot ee
13-Dec-2002 03:28
If anybody has problems with getting new curl working with older version of php then ...

I got stuck with installing curl-7.10.2+php 4.0.6 or better said configuring PHP with mentioned version of curl.

After some hopeless attempts to configure curl support into PHP I saw an error message:

"checking for cURL greater than or equal to 7.8... ./configure: line 11725: test: 070a02: integer expression expected"

after which configure said: "configure: error: cURL version 7.8 or later is required to compile php with cURL support"

On the mentioned line "test" is used to compare verion from curl-sonfig with preset value 70800 but ... test expects both values to be integer. From verson 7.8 curl outputs its version in hex...

I did'nt had the time to think about fixing this - I just deleted lines responsible and got curl working :)
jerome at blaster dot dyndns dot org
10-Oct-2002 02:23
I wanted to import an https Web pages thanks to fopen("https://xxx", "r") or fsockopen but as far as I read, it was only possible with cURL extension (to get these functions worked with https, we have to wait the PHP 4.3 release, TBC) , so I installed it and it worked. It is quite easy, but I spend to much time on it:
First of all, here is the most important: WE DON'T NEED to compile anything to get curl worked. All the extensions that we need are already in the PHP 4.2.3 zip. So here are the instructions and links to get Apache + PHP + CURL work properly:

Downloads:
=> Apache Web server with Openssl included:
 &n