 |
curl_exec (PHP 4 >= 4.0.2, PHP 5) curl_exec -- Perform a CURL session Descriptionmixed curl_exec ( resource ch )
This function should be called after you initialize a CURL
session and all the options for the session are set. Its purpose
is simply to execute the predefined CURL session (given by the
ch).
例子 1.
Initializing a new CURL session and fetching a webpage
<?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); ?>
|
|
注:
If you want to have the result returned instead of it being printed to the
browser directly, use the CURLOPT_RETURNTRANSFER option of
curl_setopt().
Jrgen Tjern
08-Mar-2005 10: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 07: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 02: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 10: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 08: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 07: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 11: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 09: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 04: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;
}
?>
|  |