Jump to content

[Solved] Problem with Imgur api using curl udf


Rex
 Share

Recommended Posts

Hi

I'm trying to convert this php

<?
$img=$_FILES['img'];
if(isset($_POST['submit'])){ 
 if($img['name']==''){  
  echo "<h2>An Image Please.</h2>";
 }else{
  $filename = $img['tmp_name'];
  $client_id="88fd52d307ecceb";
  $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'/>";
  }else{
   echo "<h2>There's a Problem</h2>";
   echo $pms['data']['error'];  
  } 
 }
}
?>

to AutoIT using the curl udf by ward

But my script hangs at this Curl_Easy_Setopt($curl, $CURLOPT_HTTPHEADER, ('Authorization: Client-ID MY_ID_HERE')

and I have to kill it manually :'(

 

My code looks like this, yet this is only at test code, and would be rewritten when/if I can get it to work

#NoTrayIcon
#include 'include/Base64.au3' ; by Ward#include 'Imgur.au3'
#include <curl.au3>
;~ #include 'include/JSON.au3' ; by Ward
Global $sFile = FileOpenDialog('Open', @ScriptDir, 'Images (*.jpg;*.gif;*.png;*.bmp)', 1)

$fp = FileOpen($sFile, 16)
        $sData = FileRead($fp)
        FileClose($fp)

        $Post = _Base64Encode($sData)
ConsoleWrite(@lf & $Post & @LF)
Local $Curl = Curl_Easy_Init()
    If Not $Curl Then Exit

Curl_Easy_Setopt($Curl, $CURLOPT_URL, "https://api.imgur.com/3/upload")
    Curl_Easy_Setopt($Curl, $CURLOPT_USERAGENT, "AutoIt/Curl")
    Curl_Easy_Setopt($curl, $CURLOPT_HTTPHEADER, 'Authorization Client-ID MY_ID_HERE') <-- Script hangs/stop at this :'(
    Curl_Easy_Setopt($Curl, $CURLOPT_ACCEPT_ENCODING, "")
    Curl_Easy_Setopt($Curl, $CURLOPT_WRITEFUNCTION, Curl_DataWriteCallback())
    Curl_Easy_Setopt($Curl, $CURLOPT_WRITEDATA, $Curl)
    Curl_Easy_Setopt($Curl, $CURLOPT_FOLLOWLOCATION, 1)
    Curl_Easy_Setopt($Curl, $CURLOPT_POST, 1)
    Curl_Easy_Setopt($curl, $CURLOPT_RETURNTRANSFER, 1)
    Curl_Easy_Setopt($Curl, $CURLOPT_COPYPOSTFIELDS, $Post)
    Curl_Easy_Setopt($Curl, $CURLOPT_TIMEOUT, 30)
    Curl_Easy_Setopt($Curl, $CURLOPT_SSL_VERIFYPEER, 0)

    Local $Code = Curl_Easy_Perform($Curl)
    If $Code <> $CURLE_OK Then ConsoleWrite(Curl_Easy_StrError($Code) & @LF)
    Local $Data = BinaryToString(Curl_Data_Get($Curl))
    Local $EffectiveUrl = Curl_Easy_GetInfo($Curl, $CURLINFO_EFFECTIVE_URL)
    Curl_Easy_Cleanup($Curl)
    Curl_Data_Cleanup($Curl)

    ConsoleWrite("Json return: " & $EffectiveUrl & @LF) ; Just to see it it works or not
    Exit

Any one have an idea to what/why the script hangs/stops at the Authorization

Using curl -k -H "Authorization: client-ID MY_ID_HERE" https://www.example.com/ works fine

 

Cheers
/Rex

Edited by Rex
Solved
Link to comment
Share on other sites

After hours of googling, and trail and errors I solved the problem.

I had to use

Local $Slist = Curl_Slist_Append(0, "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:34.0) Gecko/20100101 Firefox/34.0")
    $Slist = Curl_Slist_Append($Slist, "Authorization: client-ID MU_ID_HERE")

Cheers
/Rex

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...