downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Using PHP's cURL module to fetch the example.com homepage> <Predefined Constants
Last updated: Fri, 30 Oct 2009

view this page in

Examples

Table of Contents



add a note add a note User Contributed Notes
Examples
iim.vxk AT gmail . com
06-Oct-2009 03:38
curl couldn't connect to host - unless it is localhost ???

if U try aLL ( copying ssleay32.dll, libeay32.dll, uncommenting php.ini, and tah msvcrt.dll ) and that freakin CURL don't work on ur Windows Xp installation, ( only CURL works on requests to localhost but not external )

u pro bably are miissing something basic ::

curl_setopt($curl, CURLOPT_PROXY,"localhost:80");

( exists other way to set it )

THIS finally WORKS FOR ME, now i believe in the web appz again... ( this chunky thing takes several days of my development )

pD :: i hope i help you [ as other helped me on the past ]
vojta at draxl dot eu
16-Aug-2009 09:34
The previous solution is useful for passing through The Google Docs form and also handle the filled data at own server.

You can add the missing feature of confirmation mail with own script and more

PHP converts the dots in the variable's names to underscore. So dont forget to replace back the _ with . in the names of POST variables before sending to google.
ac at zwerg dot at
26-Jun-2009 11:46
If you want to write a sort of php wrapper to include the results of another http(s) request maybe pointing to a totally different site or just different code (mod_perl with HTML::Mason, in my case) into a php based layout, and just pass-thru all GET and POST variables to the sub-request, the following snippet can be used. Note there is no error handling, so this is subject to the underlying application.

<?php
$ch
= curl_init($sub_req_url);
$encoded = '';
// include GET as well as POST variables; your needs may vary.
foreach($_GET as $name => $value) {
 
$encoded .= urlencode($name).'='.urlencode($value).'&';
}
foreach(
$_POST as $name => $value) {
 
$encoded .= urlencode($name).'='.urlencode($value).'&';
}
// chop off last ampersand
$encoded = substr($encoded, 0, strlen($encoded)-1);
curl_setopt($ch, CURLOPT_POSTFIELDS$encoded);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_exec($ch);
curl_close($ch);
?>
randearievilo at gmail dot com
16-Jun-2008 05:39
This simple code work fine using libcurl versions before 7.18.x.

<?php
        $ch
= curl_init("www.example.com/curl.php?option=test");
       
curl_setopt($ch, CURLOPT_HEADER, 0);
       
curl_setopt($ch, CURLOPT_POST, 1);
       
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
       
$output = curl_exec($ch);      
       
curl_close($ch);
        echo
$output;
?>

If you have some trouble (Ex.: Failed to open/read local data from file/application) using a similar code with libcurl 7.18.x, try to add the option curl_setopt($ch, CURLOPT_POSTFIELDS, "").

I think there was a change in the option CURLOPT_POST. Now, to use this option, it seems to be necessary to set the option CURLOPT_POSTFIELDS.
jlee8df at gmail dot com
31-May-2008 03:21
Update to my previous code:

For every return statement, call "curl_close($ch);" or "fclose($fp);"
or both to pair up "curl_init()" and/or "fopen()".

For example:
<?php
 
// ...

  // this statement:
  // if( !curl_setopt($ch, CURLOPT_URL, $url) ) return "FAIL: curl_setopt(CURLOPT_URL)";

  // should be:
 
if( !curl_setopt($ch, CURLOPT_URL, $url) )
  {
   
fclose($fp); // to match fopen()
   
curl_close($ch); // to match curl_init()
   
return "FAIL: curl_setopt(CURLOPT_URL)";
  }

 
// ...
?>

- JLèé
jlee8df at gmail dot com
30-May-2008 08:47
Basic cURL file or page download with basic error trapping.

<?php

function cURLcheckBasicFunctions()
{
  if( !
function_exists("curl_init") &&
      !
function_exists("curl_setopt") &&
      !
function_exists("curl_exec") &&
      !
function_exists("curl_close") ) return false;
  else return
true;
}

/*
 * Returns string status information.
 * Can be changed to int or bool return types.
 */
function cURLdownload($url, $file)
{
  if( !
cURLcheckBasicFunctions() ) return "UNAVAILABLE: cURL Basic Functions";
 
$ch = curl_init();
  if(
$ch)
  {
   
$fp = fopen($file, "w");
    if(
$fp)
    {
      if( !
curl_setopt($ch, CURLOPT_URL, $url) ) return "FAIL: curl_setopt(CURLOPT_URL)";
      if( !
curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";
      if( !
curl_setopt($ch, CURLOPT_HEADER, 0) ) return "FAIL: curl_setopt(CURLOPT_HEADER)";
      if( !
curl_exec($ch) ) return "FAIL: curl_exec()";
     
curl_close($ch);
     
fclose($fp);
      return
"SUCCESS: $file [$url]";
    }
    else return
"FAIL: fopen()";
  }
  else return
"FAIL: curl_init()";
}

// Download from 'example.com' to 'example.txt'
echo cURLdownload("http://www.example.com", "example.txt");

?>

- JLèé
jamespic at gmail
15-May-2008 09:24
It only needs to be writable by the web server.
promotions at jdnash dot org
08-May-2008 03:17
If executed via a web browser, the directory in which this code is located must be world writable for the file to be created.

 
show source | credits | sitemap | contact | advertising | mirror sites