douguio0185 2017-04-21 10:50
浏览 50

使用PHP上传Imgur API标题和描述

I've used properly much time to find out how Imgur's API function, unfortunately without success.

I have a good PHP script to upload the image to Imgur and post image URL back to me again. When I upload I also want to be able to give the image title and description on Imgur.

But no matter what I have tried me, it has not managed to get it to work! Can you show me how it works? I do not quite understand Imgur's API guide.

<html>
<head>
<title>Image Upload Using Imgur API</title>
</head>
<body>
<div id="content" style="margin-top:10px;height:100%;">
<center><h1>Image Upload Using Imgur API</h1></center>
<form action="index.php" enctype="multipart/form-data" method="POST">
Choose Image : <input name="img" size="35" type="file"/>
Title : <input name="title" size="35" type="text"/><br/>
Description : <input name="description" size="35" type="text"/><br/>
<input type="submit" name="submit" value="Upload"/>
</form>
<?
if(isset($_POST['submit'])){
$img=$_FILES['img'];
if($img['name']==''){
echo "<h2>An Image Please.</h2>";
}else{
$filename = $img['tmp_name'];
$client_id="*****";//Your Client ID here
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
$pvars   = array('image': => base64_encode($data));
$timeout = 30;
$curl    = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID      ' .   $client_id));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);;
$out = curl_exec($curl);
curl_close ($curl);
$pms = json_decode($out,true);
$url=$pms['data']['link'];


if($url!=""){
 echo "<h2>Uploaded Without Any Problem</h2>";
 echo "<img src='$url'/><br>";
 echo $pms['data']['link']. "<br>";
 echo str_replace("http://","https://", "$url"); // dette er https versionen. 
 echo $pms['data']['id']. "<br>";
 echo $pms['data']['type']. "<br>";
 }else{
 echo "<h2>There's a Problem</h2>";
echo $pms['data']['error']['message'];
}
}
}
 ?>
  </div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向