Upload image on imgur with php

假装没事ソ 提交于 2019-12-14 04:03:12

问题


Trying to make script which uploading image on imgur anonymously. Just blank page when I run it. I need to post image(in base 64 string format) with title and description. Sorry for my english and code.

<?
header ("Content-type: image/png");  
    $im = ImageCreate (200, 100);     
    $couleur_fond = ImageColorAllocate ($im, 255, 0, 0);  
  $client_id="d9514d92012b55a";
  $pvars   = base64_encode($im);
  $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 . ', title: title, description: description'));
  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'];


?>

回答1:


If you're trying to print the url in the end, you should remove the header ("Content-type: image/png");.

Explanation: header ("Content-type: image/png"); tells the browser to behave with the content as image while it is a plain text or html...


You should also use full php tag <?php instead of the short one <?

Explanation: Some servers do not interpret php code starting with short php tag and send the code to browser as plain text.



来源:https://stackoverflow.com/questions/37979073/upload-image-on-imgur-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!