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

search for in the

cURL Funkcje> <Stałe predefiniowane
Last updated: Fri, 21 Nov 2008

view this page in

Przykłady

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:

Example #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($chCURLOPT_FILE$fp);
curl_setopt($chCURLOPT_HEADER0);

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



cURL Funkcje> <Stałe predefiniowane
Last updated: Fri, 21 Nov 2008
 
add a note add a note User Contributed Notes
Przykłady
randearievilo at gmail dot com
16-Jun-2008 07: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 05: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 10: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 11:24
It only needs to be writable by the web server.
promotions at jdnash dot org
08-May-2008 05: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.

cURL Funkcje> <Stałe predefiniowane
Last updated: Fri, 21 Nov 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites