使用PHP Imgurv3 API将文件上传到imgur

时间:2013-06-25 10:19:52

标签: php curl imgur

我正在编写一个将图像直接上传到imgur的webapp。由于其所有旧版本的API已被弃用,因此我被迫使用其API的v3。不幸的是,我无法让API工作。我正在使用curl来访问API。

$pvars   = array('image' => base64_encode($data));
$timeout = 30;
$curl    = curl_init();

curl_setopt($curl, CURLOPT_URL, 'https://api.imgur.com/3/upload');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID xxxxxxa61xxxxxx'));
$xml = curl_exec($curl);
$xmlsimple = new SimpleXMLElement($xml);
print gettype($xml)."<hr>";
echo '<img height="180" src="';
echo $xmlsimple->links->original;
echo '">';
curl_close ($curl);

“Xml”变量始终返回“false”,不显示服务器错误。有人可以指导我做错了什么吗?不幸的是,我也无法在文档中找到任何正确的示例,也无法指导我。

我刚刚尝试了关于堆栈溢出的另一个问题的代码,但我仍然遇到同样的问题。

    var_dump(base64_encode($data));
echo "<hr>";

$client_id = "xxxxxxa61xxxxxx";
$url = 'https://api.imgur.com/3/image.json';
$headers = array("Authorization: Client-ID $client_id");
$pvars  = array('image' => base64_encode($data));

$curl = curl_init();

curl_setopt_array($curl, array(
   CURLOPT_URL=> $url,
   CURLOPT_TIMEOUT => 30,
   CURLOPT_POST => 1,
   CURLOPT_RETURNTRANSFER => 1,
   CURLOPT_HTTPHEADER => $headers,
   CURLOPT_POSTFIELDS => $pvars
));

$json_returned = curl_exec($curl); // blank response
print_r(curl_getinfo($curl));
echo "<hr>";

echo "Result: " . $json_returned ;
echo "<hr>";
var_dump($json_returned);
echo "<hr>";

curl_close ($curl); 

0 个答案:

没有答案