Jump to content
Sign in to follow this  
AlmarM

Need help sending an image to a PHP file using WinHTTP

Recommended Posts

Hello,

For sharing screenshots with my friends I'm using uploadscreenshot.com.

I noticed they have a useable API, so I thought I could create a neat little program which will upload screenshot with a single button click.

After reading their documentation I started to play some with WinHTTP, but unfortunately I have zero experience with WinHTTP.

API documentation: www.uploadscreenshot.com/api-documentation

What I've tried:

#include "WinHTTP/WinHttp.au3"

Global Const $sAPIKey = "*snip*"
Global Const $sAPIURL = "http://img1.uploadscreenshot.com/api-upload.php"
Global Const $sIP = "209.105.243.66" ; pinged from 'uploadscreenshot.com'

Global $hOpen = _WinHttpOpen()
Global $hConnect = _WinHttpConnect($hOpen, $sAPIURL)
Global $hRequest = _WinHttpOpenRequest($hConnect, "POST", "C:\Users\Lyker\Desktop\Other\Untitled-1.png")

_WinHttpSendRequest($hRequest)
_WinHttpReceiveResponse($hRequest)

MsgBox(0, "", _WinHttpReadData($hRequest))

_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

I have no idea how I should use the WinHTTP UDF to connect with their API and send images & fill in variables. ;)

Any pokes in the right direction?

Almar

Edited by AlmarM

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Share this post


Link to post
Share on other sites

I've tried some more things and I already got some closer!

#include "WinHTTP/WinHttp.au3"

Global Const $sAPIKey = "81d63021b65545554828638091"
Global Const $sAPIURL = "/api-upload.php?apiKey=" & $sAPIKey & "&xmlOutput=1&testMode=1"
Global Const $sURL = "img1.uploadscreenshot.com"

Global $hOpen = _WinHttpOpen()
Global $hConnect = _WinHttpConnect($hOpen, $sURL)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hConnect = ' & $hConnect & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
Global $hRequest = _WinHttpOpenRequest($hConnect, "POST", $sAPIURL)

_WinHttpSendRequest($hRequest)
_WinHttpWriteData($hRequest, StringToBinary(FileRead("C:UsersLykerDesktopOtherUntitled-1.png")))
_WinHttpReceiveResponse($hRequest)

MsgBox(0, "", _WinHttpReadData($hRequest))

_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

It now returns what it should return.

The only problem now is that I don't know how to send the image using POST to that PHP file.

The XML is giving me a ERRORCODE=6. Which means the 'userfile' is empty, AKA I did not send the image correctly.

Anyone? ;)


Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Share this post


Link to post
Share on other sites

During your second post I tried something simliar but with the same result.

I tried their docu on the api....and the demo html post:

http://uploadscreenshot.com/api/test-api.html

I noticed when I select a image with a path (and the path is shown in the formular) the post message does not got the path

http://img1.uploadscreenshot.com/api-upload.php?userfile=Desert.jpg&apiKey=519acd4be68445997245348820&xmlOutput=1&imageMD5=&username=&userPasswordMD5=&testMode=1&Submit=Upload

I' not that much experienced, but it looks like the post does not take care about paths....

Sorry, not helping you. Maybe search for "POST File with path"?

Share this post


Link to post
Share on other sites

I've found something in the WinHTTP UDF post (from trancexx) in example scripts by ProgAndy.

I think that is what I need, but with my little expierence I think I cannot figure that out alone. ;)

Edited by AlmarM

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Share this post


Link to post
Share on other sites

You left your key in pal

I saw, but it doesn't matter.

You can register multiple API key's on the same e-mail, uploadscreenshot.com doesn't care about that. :)

What if I forget my API key?

Just register for a new one above.

Can I request multiple API keys for the same email address?

Yes, just submit the form as many times as needed and a new key will be sent each time. This way you can use a different API key in each of your applications.

Thanks though! ;)

EDIT:

God this is hard..

My next try:

#include "WinHTTP/WinHttp.au3"

Global Const $sAPIKey = "81d63021b65545554828638091"
Global Const $sAPIURL = "/api-upload.php?apiKey=" & $sAPIKey & "&xmlOutput=1&testMode=1"
Global Const $sURL = "img1.uploadscreenshot.com"

Global $hOpen = _WinHttpOpen()
Global $hConnect = _WinHttpConnect($hOpen, $sURL)
Global $hRequest = _WinHttpOpenRequest($hConnect, "POST", $sAPIURL)

Global $sData = ""
$sData = 'MIME-Version: 1.0' & @CRLF
$sData &= 'Content-Type: multipart/mixed; boundary=darker' & @CRLF & @CRLF ; on or off? since it's already givin inside the _WinHttpSendRequest
$sData &= '--darker' & @CRLF
$sData &= 'Content-Disposition: attachment; filename="Untitled-1.png"' & @CRLF
$sData &= 'Content-Type: image/png' & @CRLF & @CRLF
$sData &= FileRead("C:UsersLykerDesktopOtherUntitled-1.png") & @CRLF ;~ $sData &= _Base64Encode(FileRead("C:UsersLykerDesktopOtherUntitled-1.png")) & @CRLF
$sData &= '--darker--'

FileWrite(@DesktopDir & "asdas.txt", $sData)
_WinHttpSendRequest($hRequest, "Content-Type: multipart/mixed; boundary=darker", $WINHTTP_NO_REQUEST_DATA, StringLen($sData))
;~ _WinHttpWriteData($hRequest, $sData)
_WinHttpWriteDataBin($hRequest, StringToBinary($sData)) ; base 64 encode or binary send?
_WinHttpReceiveResponse($hRequest)

MsgBox(0, "", _WinHttpReadData($hRequest))

_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

Func _WinHttpWriteDataBin($hRequest, $bBinary)
    Local $lpBinary
    Local $iNumberOfBytesToWrite
    If IsDllStruct($bBinary) Then
        $lpBinary = DllStructGetPtr($bBinary)
        $iNumberOfBytesToWrite = DllStructGetSize($bBinary)
    Else
        $iNumberOfBytesToWrite = BinaryLen($bBinary)
        Local $sBinary = DllStructCreate("byte[" & $iNumberOfBytesToWrite & "]")
        DllStructSetData($sBinary, 1, $bBinary)
        $lpBinary = DllStructGetPtr($sBinary)
    EndIf

    Local $a_iCall = DllCall("Winhttp.dll", "int", "WinHttpWriteData", _
            "hwnd", $hRequest, _
            "ptr", $lpBinary, _
            "dword", $iNumberOfBytesToWrite, _
            "dword*", 0)

    If @error Or Not $a_iCall[0] Then
        Return SetError(1, 0, 0)
    EndIf

    Return SetError(0, $a_iCall[4], 1)
EndFunc

Func _Base64Encode($sData)
    Local $oXml = ObjCreate("Msxml2.DOMDocument")
    If Not IsObj($oXml) Then
        SetError(1, 1, 0)
    EndIf

    Local $oElement = $oXml.createElement("b64")
    If Not IsObj($oElement) Then
        SetError(2, 2, 0)
    EndIf

    $oElement.dataType = "bin.base64"
    $oElement.nodeTypedValue = Binary($sData)
    Local $sReturn = $oElement.Text

    If StringLen($sReturn) = 0 Then
        SetError(3, 3, 0)
    EndIf

    Return $sReturn
EndFunc   ;==>_Base64Encode
Edited by AlmarM

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Share this post


Link to post
Share on other sites

It should be something like this:

#include "WinHTTP/WinHttp.au3"

Global $sFile = "C:UsersLykerDesktopOtherUntitled-1.png"

Global Const $sAPIKey = "81d63021b65545554828638091"
Global Const $sURL = "uploadscreenshot.com"

; Session
Global $hOpen = _WinHttpOpen()
; Connection
Global $hConnect = _WinHttpConnect($hOpen, $sURL)

; Upload image
Global $sRead = _WinHttpSimpleFormFill($hConnect, "api/test-api.html", Default, _
        "apiKey", $sAPIKey, _
        "xmlOutput", 1, _
        "testMode", 1, _
        "name:userfile", $sFile)

; Close connection handle
_WinHttpCloseHandle($hConnect)
; Close session handle
_WinHttpCloseHandle($hOpen)

ConsoleWrite($sRead & @CRLF)

; Bye, bye...

WinHttp has its own help file. Use it.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Here is a complete example without using the HTML form:

#include "Z:/daten/Dokumente/AutoIt3/UDF/WinHttp.au3"
#include <Crypt.au3>

Global Const $fTestMode = True ; testmode will delete images after 15 minutes
Global Const $sAPIKey = "519acd4be68445997245348820" ; testkey, images will always be deleted after 15 minutes
Global Const $sAPIURL = "/api-upload.php"
Global Const $sURL = "img1.uploadscreenshot.com"

Global $hOpen = _WinHttpOpen("AutoIt UploadScreenShot Demo v1")
Global $hConnect = _WinHttpConnect($hOpen, $sURL)
Global $hRequest = _WinHttpOpenRequest($hConnect, "POST", $sAPIURL)

Global $sData = ""
If $fTestMode Then
    $sData &= '----------darker' & @CRLF
    $sData &= 'Content-Disposition: form-data; name="testMode"' & @CRLF & @CRLF
    $sData &= '1' & @CRLF
EndIf
$sData &= '----------darker' & @CRLF
$sData &= 'Content-Disposition: form-data; name="xmlOutput"' & @CRLF & @CRLF
$sData &= '1' & @CRLF
$sData &= '----------darker' & @CRLF
$sData &= 'Content-Disposition: form-data; name="imageMD5"' & @CRLF & @CRLF
$sData &= StringLower(Hex(_Crypt_HashFile(@ScriptDir & "testimage.png", $CALG_MD5))) & @CRLF
$sData &= '----------darker' & @CRLF
$sData &= 'Content-Disposition: form-data; name="userfile"; filename="testimage.png"' & @CRLF
$sData &= 'Content-Type: image/png' & @CRLF & @CRLF
$sData &= FileRead(@ScriptDir & "testimage.png") & @CRLF ;~ $sData &= _Base64Encode(FileRead("C:UsersLykerDesktopOtherUntitled-1.png")) & @CRLF
$sData &= '----------darker--'

_WinHttpSendRequest($hRequest, "Content-Type: multipart/form-data; boundary=--------darker", Binary($sData))
_WinHttpReceiveResponse($hRequest)

$sResult = _WinHttpReadData($hRequest)
MsgBox(0, "", $sResult)
ConsoleWrite($sResult & @CRLF)

_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

Awe-some work, both samples work like a charm! ;)

I personally don't like filling in forms, so I'll use ProgAndy's sample.

But I've learned a lot from both samples. Thanks!


Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Colduction
      Hi AutoIt Members and Programmers, i have a problem with Telegram UDF that does not work on some of my servers, Telegram is not restricted in these machines, here is console output in Windows 7 (Server):
      >"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "C:\Users\.NetFramework\Desktop\telegram-udf-autoit-master\tests\Test.au3" /UserParams +>20:20:40 Starting AutoIt3Wrapper (19.1127.1402.0} from:SciTE.exe (4.2.0.0) Keyboard:00000429 OS:WIN_7/Service Pack 1 CPU:X64 OS:X64 Environment(Language:0409) CodePage:0 utf8.auto.check:4 +> SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE UserDir => C:\Users\.NetFramework\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper SCITE_USERHOME => C:\Users\.NetFramework\AppData\Local\AutoIt v3\SciTE >Running AU3Check (3.3.14.5) from:C:\Program Files (x86)\AutoIt3 input:C:\Users\.NetFramework\Desktop\telegram-udf-autoit-master\tests\Test.au3 +>20:20:40 AU3Check ended.rc:0 >Running:(3.3.14.5):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\Users\.NetFramework\Desktop\telegram-udf-autoit-master\tests\Test.au3" +>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop. Test file for Telegram UDF (https://github.com/xLinkOut/telegram-udf-autoit). This file need a valid ChatID of a Telegram user who has already sent at least a message to the bot, and a valid token given by @BotFather. Insert this data in the source code. "C:\Users\.NetFramework\Desktop\telegram-udf-autoit-master\src\Telegram.au3" (1098) : ==> The requested action with this object has failed.: $oHTTP.Send() $oHTTP^ ERROR ->20:20:41 AutoIt3.exe ended.rc:1 +>20:20:41 AutoIt3Wrapper Finished. >Exit code: 1 Time: 1.56 It's really annoying problem in WinHTTP
    • By nacerbaaziz
      goodmorning autoit team
      today am comming with some winhttp problems, i hope that you can help me to solve them.
      the first problem
      is when opening a request
      my forums api allow me to delete any post using the api key
      all functions work, i mean post / get
      but when i tried to use the delete verb it's gave me an html 404 error
      here is what am tried
      #include "WinHttp.au3" ; Open needed handles Global $hOpen = _WinHttpOpen() Global $hConnect = _WinHttpConnect($hOpen, "xxxxxxxx.com") ; Specify the reguest: Global $hRequest = _WinHttpOpenRequest($hConnect, "Delete", "/vb/Api/posts/10447/?hard_delete=true", default, default) _WinHttpAddRequestHeaders($hRequest, "XF-Api-Key:xxxxx") _WinHttpAddRequestHeaders($hRequest, "XF-Api-User:xxxxx") ; Send request _WinHttpSendRequest($hRequest) ; Wait for the response _WinHttpReceiveResponse($hRequest) Global $sHeader = 0, $sReturned = 0 ; If there is data available... If _WinHttpQueryDataAvailable($hRequest) Then $sHeader = _WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_CONTENT_DISPOSITION) ;Or maybe: ; $sHeader = _WinHttpQueryHeaders($hRequest, BitOR($WINHTTP_QUERY_RAW_HEADERS_CRLF, $WINHTTP_QUERY_CUSTOM), "Content-Disposition") Do $sReturned &= _WinHttpReadData($hRequest) Until @error msgBox(64, "", $sReturned) endIf ; Close handles _WinHttpCloseHandle($hRequest) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hOpen)  
      and here is the error message
      <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>403 Forbidden</title> </head><body> <h1>Forbidden</h1> <p>You don't have permission to access /vb/Api/posts/10447/ on this server.<br /> </p> </body></html>  
      i hope you can help me 
      thanks in advance
    • By HansHenrik
      here's some PHP code to access AutoIt functions from php: 
      https://github.com/divinity76/autoit_php -
       
      as of writing, only 5 functions are added: MouseMove and MouseClick and _ScreenCapture_Capture and WinWaitActive and Send,

      it also has support for running in Cygwin (which is where i've developed it, but it *should*, in theory, run on normal php-cli for windows as well.), is there any community interest for this? 
       
      example usage
      <?php require_once("autoit.class.php"); $au = new AutoIt(); $au->MouseMove(10, 10, 5); $au->MouseClick("left"); echo "waiting up to 5 seconds for notepad window.."; if($au->WinWaitActive("[CLASS:Notepad]","",5)){ echo "found notepad!\n"; $au->Send("hello from autoit_php"); }else{ echo "timed out while waiting for notepad.\n"; } $imageBinary = $au->_ScreenCapture_Capture(); var_dump(strlen($imageBinary), imagecreatefromstring($imageBinary));  

      (i wanted to use some autoit functions from php-cli, and google wasn't of much help this time..)
    • By Surya
      Hi everyone its been loooong since I posted here 
      I have been trying to convert this curl executable parameters into autoit using the winhttp com object;
      curl -F data_file=@my_audio_file.mp3 -F model=en-US "https://api.speechmatics.com/v1.0/user/41049/jobs/?auth_token=MmQ5MTk4jdsgjhgghstOGU5YS00OWFhLWghdgjshgdhbshj017###" any ideas guys
       
      PS: I am excited to post here after a looong time
    • By Ascer
      Hello
      Opertation Sys: Win7 x64
      Problem: Connecting to webs using TLS 1.1 +
      Description: WinHttp.WinHttpRequest.5.1 using TLS 1.0 by default, i need higher version to connect into some webs.
      Dim $oHttp = ObjCreate("WinHTTP.WinHTTPRequest.5.1") $oHttp.open ("GET", "https://howsmyssl.com/a/check", False) $oHttp.Option(9) = 128 ; 128 - TLS 1.0, 512 - TLS 1.1, 2048 - TLS 1.2, 2056 - TLS 1.1 & TLS 1.2 $oHttp.Send ConsoleWrite($oHttp.responseText & @CRLF) ; at end of the respond you can check your TLS version. Mine is: {"tls_version":"TLS 1.0","rating":"Bad"} Error: $oHttp.Option works only with parameter 128 (TLS 1.0) other values make error {Bad parameter}
      Additional: I've done this tutorial about enabling TLS in registry: <link>
      Thanks for support.
      Ascer
×
×
  • Create New...