Jump to content

[Solved] Problem with Imgur api using curl udf

Recommended Posts



I'm trying to convert this php

  echo "<h2>An Image Please.</h2>";
  $filename = $img['tmp_name'];
  $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);
   echo "<h2>Uploaded Without Any Problem</h2>";
   echo "<img src='$url'/>";
   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

#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)

        $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)

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

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



Edited by Rex

Share this post

Link to post
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")


Share this post

Link to post
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


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.