Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : CURL, Client URL Library Functions : curl_exec

curl_exec

Perform a cURL session (PHP 4 >= 4.0.2, PHP 5)
mixed curl_exec ( resource ch )

Example 430. Fetching a web page

<?php
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>

Related Examples ( Source code ) » curl_exec



Code Examples / Notes » curl_exec

fifa_2k -at- sina -dot- com

With php 4.3.9 or higher,you can upload file to ftp server on win32 system
<?php
function curl_upload($src) {
$fn = basename($src);
$dest = "ftp://user:passwd@server.com/incoming/$fn";
$ch = curl_init();

$fp = fopen($src,"r");
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLE_OPERATION_TIMEOUTED, 300);
curl_setopt($ch, CURLOPT_URL, $dest);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($src));
curl_exec($ch);
fclose ($fp);
$errorMsg = '';
$errorMsg = curl_error($ch);
$errorNumber = curl_errno($ch);
curl_close($ch);
return $errorNumber;
}
?>
lower version php (I tried on php 4.3.3) on win32 can't do this and may cause php crash even you use CURLOPT_READFUNCTION.


lukasl

Thank you for sharing this.  I was wondering why my result was 1.
To get around this in a safe way, this is how I check if the result is valid.
$ch = curl_init(); /// initialize a cURL session
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$xmlResponse = curl_exec ($ch);
curl_close ($ch);
if (!is_string($xmlResponse) || !strlen($xmlResponse)) {
return $this->_set_error( "Failure Contacting Server" );
} else {
return $xmlResponse;
}


landon

If your curl installation is not compiled with SSL support you will beat your head against a wall when trying to figure out why curl_exec() is failing to fail or do anything else ...
If you run into a situation where your call to curl_exec is not returning anything you should try the same call with the command line curl


jørgen tjernø

If you've got problems with curl_exec not working, you should rather check curl_errno and curl_error than using commandline curl, like so:
(since this is easier, and also allows you to check for errors runtime, which is a vital part of any well-design piece of code. ;)
<?php
$creq = curl_init();
curl_setopt($creq, CURLOPT_URL, "http://www.foo.internal");
curl_exec($creq);
/* To quote curl_errno documentation:
    Returns the error number for the last cURL operation on the resource ch, or 0 (zero) if no error occurred. */
if (curl_errno($creq)) {
   print curl_error($creq);
} else {
   curl_close($creq);
}
?>


sharky

If You want to hide result which return curl_exec
Use bufeered output.
For example:
-----------------------
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://url.com/index.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "a=3&b=5");
//--- Start buffering
ob_start();
curl_exec ($ch);
//--- End buffering and clean output
ob_end_clean();
curl_close ($ch);
------------------
maybe it help somebody :))
----------
Best Regards
 Sharky


florian holzhauer

If you use apache2+mod_chroot with php5, add
LoadFile /lib/libnss_dns.so.2
to your mod_chroot config - this should resolver problems.


test

If you see a "0" at the end of the output, you might want to switch to HTTP/1.0:
curl_setopt($ch, CURLOPT_HTTP_VERSION, 1.0);


sybren

If you see a "0" at the end of the output, you might want to switch to HTTP/1.0:
curl_setopt($ch, CURLOPT_HTTP_VERSION, 1.0);


csaba

If you retrieve a web page and print it (so you can see it in your browser), and the page has an expiration, this expiration now applies to MyProgram.php and next time your program/page is called, even if it's grabbing a different web page, it will show what it just displayed.  In Netscape you can get rid of this by going into Edit, Options, Advanced, Cache, and clear out the Disk Cache.  But this is really annoying after short order.  The following prevents the above scenario:
<?php
function GetCurlPage ($pageSpec) {
   $ch = curl_init($pageSpec);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   $tmp = curl_exec ($ch);
   curl_close ($ch);
// if you intend to print this page, better clear out expiration tag
   $tmp = preg_replace('/(?s)<meta http-equiv="Expires"[^>]*>/i', '', $tmp);
   return $tmp;
}
?>


nagyp

fyi:
It returns false if there's an error while executing the curl session, no matter how CURLOPT_RETURNTRANSFER is set.


ppruett

fyi - if you are having problems getting a
webpage to display in your webpage with
curl_setopt(CURLOPT_RETURNTRANSFER, 1);
due to version bugginess perhaps,
you may can use output control functions
like this to show a web page
inside your webpage:
<html><head><title>whatever</title></head>
<body>
<script language="php">
$ch = curl_init("http://www.cocoavillage.com/");
// use output buffering instead of returntransfer -itmaybebuggy
ob_start();
curl_exec($ch);
curl_close($ch);
$retrievedhtml = ob_get_contents();
ob_end_clean();
// if you intend to print this page with meta tags, better clear out any expiration tag
//    $result = preg_replace('/(?s)<meta http-equiv="Expires"[^>]*>/i', '', $retrievedhtml);
// for now I just want what is between the body tags so need
// somehow cut the header footer    
$bodyandend = stristr($retrievedhtml,"<body");
// not needed- $positionstartbodystring = strlen($retrievedhtml)-strlen($bodyandend);
$positionendstartbodytag = strpos($bodyandend,">") + 1;
// got to change all to lowercase temporarily
// because end body may be upperlowercasemix
// to bad strirstr does not exist
$temptofindposition=strtolower($bodyandend);
$positionendendbodytag=strpos($temptofindposition,"</body");
//now to get the endbetween body tags
$grabbedbody=substr($bodyandend,
    $positionendstartbodytag,
          $positionendendbodytag);
//be sure to fix syntax broke by display on phpwebsite... like above line
print("$grabbedbody");
</script>
</body></html>
tada


colins

Checking the source, curl_exec seems to return FALSE on failure, TRUE on success (unless CURLOPT_RETURNTRANSFER is set 1, and then it returns the returned data).

04-oct-2006 09:41

Be careful when using curl_exec() and the CURLOPT_RETURNTRANSFER option. According to the manual and assorted documentation:
Set CURLOPT_RETURNTRANSFER to TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
When retrieving a document with no content (ie. 0 byte file), curl_exec() will return bool(true), not an empty string. I've not seen any mention of this in the manual.
Example code to reproduce this:
<?php
// fictional URL to an existing file with no data in it (ie. 0 byte file)
$url = 'http://www.example.com/empty_file.txt';
$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
// execute and return string (this should be an empty string '')
$str = curl_exec($curl);
curl_close($curl);
// the value of $str is actually bool(true), not empty string ''
var_dump($str);
?>


cameron

A slightly more efficient way to return the HTTP code would be to use the curl_getinfo function
   function getHttpResponseCode($url)
   {
       $ch = @curl_init($url);
       @curl_setopt($ch, CURLOPT_HEADER, TRUE);
       @curl_setopt($ch, CURLOPT_NOBODY, TRUE);
       @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
       @curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
       $status = array();
       $status = @curl_getinfo($ch);
       return $status['http_code'];
   }


shanto

A function to retrieve the status code of an HTTP request using CURL:
   function getHttpResponseCode($url)
   {
       $ch = @curl_init($url);
       @curl_setopt($ch, CURLOPT_HEADER, TRUE);
       @curl_setopt($ch, CURLOPT_NOBODY, TRUE);
       @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
       @curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
       $status = array();
       $response = @curl_exec($ch);
       preg_match('/HTTP\/.* ([0-9]+) .*/', $response, $status);
       return $status[1];
   }


Change Language


Follow Navioo On Twitter
Constants
curl_close
curl_copy_handle
curl_errno
curl_error
curl_exec
curl_getinfo
curl_init
curl_multi_add_handle
curl_multi_close
curl_multi_exec
curl_multi_getcontent
curl_multi_info_read
curl_multi_init
curl_multi_remove_handle
curl_multi_select
curl_setopt_array
curl_setopt
curl_version
eXTReMe Tracker