Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过PHP和cURL上传IMGUR文件_Php_Curl_Imgur - Fatal编程技术网

通过PHP和cURL上传IMGUR文件

通过PHP和cURL上传IMGUR文件,php,curl,imgur,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); $timeo

我正在尝试通过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, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);

$xml = curl_exec($curl);

curl_close ($curl);

这是我收到的错误消息:

警告:fopen(image.jpg)无法打开流:没有这样的文件或目录

我不明白这个部分:$filename=“image.jpg”; 文件名来自哪里,因为它是base64生成的字符串? 谢谢
Bob

该警告是因为fopen正试图从运行脚本的目录中读取文件image.jpg。 这里可以看到一个关于如何通过curl传输文件的好例子

其中有$localFile=$\u文件[$fileKey]['tmp\u name'];您可以将$localFile='/path/to/image.jpg';以及更改服务器信息并添加可能需要传递给imgur的任何其他变量。

更改第1行:

$filename = "image.jpg";
致:

然后,发布。。。我推荐一种类似的形式:

    <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_密钥。 您可以在此处获取API密钥:

最后,您的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, CURLOPT_URL, 'http://api.imgur.com/2/gallery.xml');
    curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
    $xml = curl_exec($curl);
    $xmlsimple = new SimpleXMLElement($xml);
    echo '<img height="180" src="';
    echo $xmlsimple->links->original;
    echo '">';

    curl_close ($curl);
    }
?>

这意味着PHP找不到'image.jpg',因此无法“打开”它。如果图像文件与此脚本不在同一目录中,则必须指定指向图像的路径。绝对路径(
/path/to/image
)或相对于脚本的路径(
./../where/image.jpg
)。我不知道“image.jpg”来自何处。据我所知,PHP是从我发送的base64字符串生成的,然后把它放在PHP文件所在的文件夹中,对吗?好的,我想我找到了。只需删除此部分:
$filename=“image.jpg”$handle=fopen($filename,“r”)$data=fread($handle,filesize($filename))但是仍然有一个问题没有解决-我如何从IMGUR接收到带有上传图像路径的回调或响应?您将得到一个xml响应。您需要从保存在变量$xml中的响应中取出URL标记。文档在这里:我使用ActionScript3(AS3)和PHP来上传图像。我从IMGUR示例中获取了这一点,但它只返回空:
var-ireresponse:XML=newxml(unescape(loader.data))有什么想法吗?如何将PHP的响应发送到Flash?
    <?
    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, CURLOPT_URL, 'http://api.imgur.com/2/gallery.xml');
    curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
    $xml = curl_exec($curl);
    $xmlsimple = new SimpleXMLElement($xml);
    echo '<img height="180" src="';
    echo $xmlsimple->links->original;
    echo '">';

    curl_close ($curl);
    }
?>