suche nach in der

curl_getinfo> <curl_error
Last updated: Fri, 18 May 2012

view this page in

curl_exec

(PHP 4 >= 4.0.2, PHP 5)

curl_execEine cURL-Session ausführen

Beschreibung

mixed curl_exec ( resource $ch )

Ausführen der übergegebenen cURL-Session.

Diese Funktion sollte aufgerufen werden, nachdem eine cURL-Session initialisiert wurde und alle Optionen für jene gesetzt sind.

Parameter-Liste

ch

Ein von curl_init() zurückgegebenes cURL-Handle.

Rückgabewerte

Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben. Ist jedoch die Option CURLOPT_RETURNTRANSFER gesetzt so wird das Ergebnis des cURL-Transfers zurückgegeben, im Falle eines Fehlers FALSE.

Beispiele

Beispiel #1 Abrufen einer Webseite

<?php
 
// erzeuge einen neuen cURL-Handle
 
$ch curl_init();

 
// setze die URL und andere Optionen
 
curl_setopt($chCURLOPT_URL"http://www.example.com/");
 
curl_setopt($chCURLOPT_HEADER0);

 
// führe die Aktion aus und gebe die Daten an den Browser weiter
 
curl_exec($ch);

 
// schließe den cURL-Handle und gebe die Systemresourcen frei
 
curl_close($ch);
 
?>

Siehe auch



add a note add a note User Contributed Notes
curl_exec
lukasl at ackleymedia dot com
06-Oct-2006 04:52
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;
}
04-Oct-2006 10: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);

?>
Jørgen Tjernø
08-Mar-2005 03:12
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);
}
?>
landon at phazeforward dot com
01-Dec-2004 12:43
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
fifa_2k [-at-] sina [-dot-] com
29-Oct-2004 08:29
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.
sybren at thirdtower dot com
01-Jul-2003 04:00
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);
nagyp at hunaxon dot hu
27-Apr-2003 02:22
fyi:
It returns false if there's an error while executing the curl session, no matter how CURLOPT_RETURNTRANSFER is set.
colins at infofind dot com
25-Apr-2002 01:42
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).
ppruett at webengr dot com
01-Mar-2002 04:06
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
sharky at im dot net dot ua
22-Nov-2001 02:08
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
csaba at alum dot mit dot edu
22-May-2001 10:06
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;
}
?>

curl_getinfo> <curl_error
Last updated: Fri, 18 May 2012