使用imgur API V3上传匿名使用PHP图片 [英] using imgur api v3 to upload images anonymously using php

查看:846
本文介绍了使用imgur API V3上传匿名使用PHP图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算上传图片到imgur匿名使用它的API,我注册了我的匿名上传类应用程序,并得到了客户端ID和客户端密钥,如何使用PHP来上传图片到imgur和检索直接链接到图像?
任何人都可以提出链接的任何的例子吗?
这就是我试图做的,但我得到的错误致命错误:超过了最长30秒的执行时间

 < PHP$ CLIENT_ID =:CLIENT_ID; //把你的API密钥这里
$文件名=图像/ q401x74ua3402.jpg
$处理=的fopen($文件名,R);
$数据= FREAD($处理,档案大小($文件名));// $的数据是文件数据
$ pvars =阵列('形象'=> base64_en code($数据),关键= GT; $ CLIENT_ID);
$超时= 30;
$卷曲= curl_init();curl_setopt($卷曲,CURLOPT_URL,https://api.imgur.com/3/upload.json');
curl_setopt($卷曲,CURLOPT_TIMEOUT,$超时);
curl_setopt($卷曲,CURLOPT_POST,1);
curl_setopt($卷曲,CURLOPT_RETURNTRANSFER,1);
curl_setopt($卷曲,CURLOPT_POSTFIELDS,$ pvars);
$ XML = curl_exec($卷曲);
$ xmlsimple =新的SimpleXMLElement($ XML);
回声'&下; IMG高度=180SRC =';
回声$ xmlsimple->链接 - >原件;
回声'>';curl_close($卷曲);?>


解决方案

发现错误,我需要发送的授权细节的头,
例如:code

 < PHP
$ CLIENT_ID ='XXXXXXXX';$文件=的file_get_contents(测试image.png);$ URL ='https://api.imgur.com/3/image.json';
$头=阵列(授权:客户ID $ CLIENT_ID);
$ pvars =阵列('形象'=> base64_en code($文件));$卷曲= curl_init();curl_setopt_array($卷曲,阵列(
   CURLOPT_URL => $网址,
   CURLOPT_TIMEOUT => 30,
   CURLOPT_POST => 1,
   CURLOPT_RETURNTRANSFER => 1,
   CURLOPT_HTTPHEADER => $头,
   CURLOPT_POSTFIELDS => $ pvars
));$ json_returned = curl_exec($卷曲); //空白响应
回声结果。 $ json_returned;curl_close($卷曲);?>

I am planning to upload images to imgur anonymously using its api, i registered my application in the anonymous upload category and got client id and client secret, How to use php to upload image to imgur and retrieve direct url to the image? can anyone suggest links to any example? this is what I have tried to do but i get the error "Fatal error: Maximum execution time of 30 seconds exceeded"

<?php

$client_id = :client_id; //put your api key here
$filename = "images/q401x74ua3402.jpg";
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));

//$data is file data
$pvars   = array('image' => base64_encode($data), 'key' => $client_id);
$timeout = 30;
$curl    = curl_init();

curl_setopt($curl, CURLOPT_URL, 'https://api.imgur.com/3/upload.json');
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);

?>

解决方案

found the error, I need to send authorization details as header, eg code

<?php
$client_id = 'xxxxxxxx';

$file = file_get_contents("test-image.png");

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

$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
echo "Result: " . $json_returned ;

curl_close ($curl); 

?>

这篇关于使用imgur API V3上传匿名使用PHP图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆