繁体   English   中英

使用cURL将带有PHP的文件上传到ima​​geshack

[英]Uploading file with PHP to imageshack using cURL

我正在尝试使用以下脚本将文件上传到ima​​geshack:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://post.imageshack.us/');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
    'fileupload'=> '@../../resources/images/cancel.png',
    'optsize' => 'resample',
    'rembar' => '1'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
curl_close($ch);

echo $response;

但是,我从imageshack得到以下错误:

Wrong file type detected for file cancel.png:application/octet-stream

为什么,以及如何解决? 提前致谢!

编辑:当我使用JPG或GIF(甚至是动画的,虽然Imageshack然后去动画它们,我认为仅使用第一帧)时它确实有效,但不是使用PNG。

编辑2:我发现了如何解决它。 如前所述,它只接受JPG和GIF。 因此,我获取需要上传的图像的位置,将其复制到结尾为.jpg的临时位置,上传文件,然后在完成后,从临时位置删除该文件。

将Content-type设置为文件类型。

根据手册,它必须是一个数组或对象。 所以在这里

$arr = array('Content-type: text/plain') ;
curl_setopt ( $ch , CURL_HTTPHEADER , $arr );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM