开发者

Posting Files Using cURL using PHP

开发者 https://www.devze.com 2023-03-28 09:59 出处:网络
I am having problems posting files to an api I have built using cURL. I can post small images (say 4KB) but when I try and attach a large image I get the following error:

I am having problems posting files to an api I have built using cURL.

I can post small images (say 4KB) but when I try and attach a large image I get the following error:

Curl error: failed creating formpost data

The code I am using to make the cURL call is below:

$ch = curl_init();
curl_setopt($ch, CURLOP开发者_Go百科T_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 100); 
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");

try {  
    switch($type) {
        case "GET":
            break;
        case "POST":
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
            break;
        case "PUT":
            $fh = fopen('php://memory', 'rw'); 
            $data = http_build_query($vars, '', '&');
            fwrite($fh, $data);  
            rewind($fh);  
            curl_setopt($ch, CURLOPT_INFILE, $fh);  
            curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data));  
            curl_setopt($ch, CURLOPT_PUT, true);
            break;
        case "DELETE":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
            break;  
        default:  
            throw new InvalidArgumentException('Current verb is an invalid REST verb.'); 
    }
} catch (InvalidArgumentException $e) {  
    curl_close($ch);  
    throw $e;  
} catch (Exception $e) {  
    curl_close($ch);  
    throw $e;
} 
$result = curl_exec($ch);
$info = curl_getinfo($ch);
if(curl_errno($ch)) {
    echo 'Curl error: ' . curl_error($ch);
}

curl_close($ch);

Is there a cap on the file size for uploading an image through cURL.

Your help would be appreciated.


You may be running into a limit thats set on the server. Apache has the LimitRequestBody directive to allow the server admin to restrict upload sizes. You can read more about it here: http://httpd.apache.org/docs/2.1/mod/core.html#limitrequestbody

Server admins can apply Limits on Windows IIS services as well although only reliably from 6.0 onwards.

0

精彩评论

暂无评论...
验证码 换一张
取 消