note 52675 added to function.curl-setopt
| From: | ikendraatykendotnospamdotorg at osu1 dot php dot net | Date: | Mon, 09 May 2005 16:26:42 +0000 |
| Subject: | note 52675 added to function.curl-setopt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-89164@lists.php.net to get a copy of this message | ||
Using cURL, I needed to call a third-party script which was returning binary data as attachment to
pass on retrieved data again as attachment.
Problem was that the third-party script occassionally returned HTTP errors and I wanted to avoid
passing on zero-length attachment in such case.
Combination of using CURLOPT_FAILONERROR and CURLOPT_HEADERFUNCTION callback helped to process the
third-party script HTTP errors neatly:
<?
function curlHeaderCallback($resURL, $strHeader) {
if (preg_match('/^HTTP/i', $strHeader)) {
header($strHeader);
header('Content-Disposition: attachment; filename="file-name.zip"');
}
return strlen($strHeader);
}
$strURL = 'http://www.somesite.com/script-whichs-dumps-binary-attachment.php';
$resURL = curl_init();
curl_setopt($resURL, CURLOPT_URL, $strURL);
curl_setopt($resURL, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($resURL, CURLOPT_HEADERFUNCTION, 'curlHeaderCallback');
curl_setopt($resURL, CURLOPT_FAILONERROR, 1);
curl_exec ($resURL);
$intReturnCode = curl_getinfo($resURL, CURLINFO_HTTP_CODE);
curl_close ($resURL);
if ($intReturnCode != 200) {
print 'was error: ' . $intReturnCode;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.curl-setopt.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52675
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52675&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52675&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52675&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52675&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52675&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52675&report=yes
Search -- http://master.php.net/manage/user-notes.php