如何解决通过PHP和cURL上传IMGUR文件
| 我正在尝试通过PHP将图像上传到IMGUR。 这是代码:<?
$filename = \"image.jpg\";
$handle = fopen($filename,\"r\");
$data = fread($handle,filesize($filename));
// $data is file data
$pvars = array(\'image\' => base64_encode($data),\'mykey\' => IMGUR_API_KEY);
$timeout = 30;
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,\'http://api.imgur.com/2/upload.xml\');
curl_setopt($curl,CURLOPT_TIMEOUT,$timeout);
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,CURLOPT_POSTFIELDS,$pvars);
$xml = curl_exec($curl);
curl_close ($curl);
?>
这是我收到的错误消息:
警告:fopen(image.jpg)无法打开流:无此文件或目录
我不明白这部分:$ filename = \“ image.jpg \”;
由于文件名是base64生成的字符串,因此文件名来自何处?
谢谢,
鲍勃
解决方法
该警告是因为fopen试图从运行脚本的目录中读取文件image.jpg。
关于如何通过curl传输文件的一个很好的例子可以在这里看到
通过cURL从PHP中的POST表单发送文件
他们在哪里$ localFile = $ _FILES [$ fileKey] [\'tmp_name \'];您将把$ localFile = \'/ path / to / image.jpg \';除了更改服务器信息并添加其他任何变量外,您可能还需要传递给imgur。
,将第1行更改为:
$filename = \"image.jpg\";
至:
$filename = $_FILES[\'uploaded_file\'][\'tmp_name\'];
然后,要发布...我建议使用类似于以下的形式:
<form enctype=\"multipart/form-data\" method=\"post\" action=\"upload.php\" target=\"my_iframe\">
Choose your file here:
<input name=\"uploaded_file\" type=\"file\"/>
<input type=\"submit\" value=\"Upload It\"/>
</form>
<!-- when the form is submitted,the server response will appear in this iframe -->
<script language=\"JavaScript\">
<!--
function autoResize(id){
var newheight;
var newwidth;
if(document.getElementById){
newheight=document.getElementById(id).contentWindow.document .body.scrollHeight;
newwidth=document.getElementById(id).contentWindow.document .body.scrollWidth;
}
document.getElementById(id).height= (newheight) + \"px\";
document.getElementById(id).width= (newwidth) + \"px\";
}
//-->
</script>
<IFRAME name=\"my_iframe\" width=\"100%\" height=\"200px\" id=\"iframe1\" marginheight=\"0\" frameborder=\"0\" onLoad=\"autoResize(\'iframe1\');\"></iframe>
如果您将所有php都放到upload.php中,然后将该表单放在同一目录中的页面上,则它几乎可以正常工作了……除非您的源代码中还没有API_KEY。
您可以在此处获取API密钥:https://imgur.com/register/api_anon
最后,您的php应该如下所示:
<?
if( isset($_FILES[\'uploaded_file\']) )
{
$IMGUR_API_KEY = \'u432ewriuq3oirefuie\'; //put your api key here
$filename = $_FILES[\'uploaded_file\'][\'tmp_name\'];
$handle = fopen($filename,\"r\");
$data = fread($handle,filesize($filename));
//$data is file data
$pvars = array(\'image\' => base64_encode($data),\'key\' => $IMGUR_API_KEY);
#$pvars = array(\'key\' => $IMGUR_API_KEY);
$timeout = 30;
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,\'http://api.imgur.com/2/upload.xml\');
#curl_setopt($curl,\'http://api.imgur.com/2/gallery.xml\');
curl_setopt($curl,CURLOPT_TIMEOUT,$timeout);
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,CURLOPT_POSTFIELDS,$pvars);
$xml = curl_exec($curl);
$xmlsimple = new SimpleXMLElement($xml);
echo \'<img height=\"180\" src=\"\';
echo $xmlsimple->links->original;
echo \'\">\';
curl_close ($curl);
}
?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。