note 87015 added to function.curl-exec
| From: | MarkOmohundro at osu1 dot php dot net | Date: | Fri, 14 Nov 2008 18:09:25 +0000 |
| Subject: | note 87015 added to function.curl-exec | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-146556@lists.php.net to get a copy of this message | ||
Just a simple curl_exec() example with a twist: I use the familiar http response status code
returned from curl_getinfo() to put either data, or a custom error message + curl_error() into a
single string. I find this useful when the response ends up in a <div> or <span> tag.
<?php
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
...
$output = curl_exec($ch);
$info = curl_getinfo($ch);
if ($output === false || $info['http_code'] != 200) {
$output = "No cURL data returned for $url [". $info['http_code'].
"]";
if (curl_error($ch))
$output .= "\n". curl_error($ch);
}
else {
// 'OK' status; format $output data if necessary here:
...
}
// then return or display the single string $output
?>
----
Server IP: 69.147.83.197
Probable Submitter: 4.232.93.28
----
Manual Page -- http://www.php.net/manual/en/function.curl-exec.php
Edit -- https://master.php.net/note/edit/87015
Del: integrated -- https://master.php.net/note/delete/87015/integrated
Del: useless -- https://master.php.net/note/delete/87015/useless
Del: bad code -- https://master.php.net/note/delete/87015/bad+code
Del: spam -- https://master.php.net/note/delete/87015/spam
Del: non-english -- https://master.php.net/note/delete/87015/non-english
Del: in docs -- https://master.php.net/note/delete/87015/in+docs
Del: other reasons-- https://master.php.net/note/delete/87015
Reject -- https://master.php.net/note/reject/87015
Search -- https://master.php.net/manage/user-notes.php