note 52675 modified in function.curl-setopt by danbrown

From: Date: Sun, 17 May 2009 17:32:07 +0000
Subject: note 52675 modified in function.curl-setopt by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-154995@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: <?php 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.example.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; } ?> --was-- 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; } ?> http://php.net/manual/en/function.curl-setopt.php

« previous php.notes (#154995) next »