4

I have the task of converting the following block of PHP code to perform an HTTP multipart post. I understand that basics of doing HTTP posts (my code already does a post of some JSON) and I've read up on the basics of multipart requests. However, Curl is obviously doing a lot of leg work behind the scenes there which will have to be converted into a .NET stream write. However, with these things, the devil is in the detail.

Not being familiar with Curl and being a PHP novice, is there a way, using the code, to see the request that's finally sent with the curl_exec? Having a real life example would really help. In fact, I'm okay with the simple fields - each one having a separate boundary/part. It's the JPEG image encoding that's the tricky one for me. I could set-up a PHP environment and somehow debug it? I infer that Curl is sort of web client, i.e. it does much of what a web browser does without a user interface.

<?php

    function AddPhoto( $account, $password, $pubId, $photoId, $owner, $pubDate, $attribution, $caption, $keywords, $photoPath )
    {
        // set up the arguments of the multipart form data
        $args = array( 'Method' => 'AddPhoto', 'Account' => 'Test', 'Password' => 'pw', 

                      'PubID' => 'WOR/1002',
                      'PhotoID' => '12345',
                      'Owner' => 'me',
                      'PubDate' => '2015-07-01',
                      'Attribution' => '',
                      'Caption' => 'the front door',
                      'Keywords' => 'Pub',

                      'Photo' => new CurlFile( $photoPath,              // path to the file
                                                'image/jpeg',               // MIME type of the file
                                                'image'                     // file name (not used)
                                             )
                    );

        // create a curl request object
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, 'http://dev.camrapubs.org.uk/simon/PubDatabase/API2.php');
        curl_setopt( $ch, CURLOPT_POST, 1 );                // use the POST method
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $args );      // supply the arguments for the POST
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );      // ensure curl_exec returns the result rather than echoing it

        // make the call and get the result
       return curl_exec($ch);
    }

    $result = AddPhoto( ‘xx', ‘xx', 'WOR/1002', '12345', 'me', '2015-07-01', 'copyleft', 'The front door', 'Pub', 'sample.jpg' );
    echo 'result = ', $result, PHP_EOL;
?>
Rob Nicholson
  • 1,943
  • 5
  • 22
  • 37
  • Anything here helpful? http://stackoverflow.com/questions/3757071/php-debugging-curl – Kevin_Kinsey Sep 09 '15 at 21:56
  • Thanks - that's definitely barking up the right tree - will check it out. Time to build a PHP development environment I think – Rob Nicholson Sep 09 '15 at 22:04
  • http://stackoverflow.com/questions/21905942/posting-raw-image-data-as-multipart-form-data-in-curl? – CodeCaster Sep 09 '15 at 22:06
  • My investigations have also revealed a Windows version of the Curl library (libcurl.dll) which means I might be able to use the above almost verbatim - once getting over the shock of having to go back to the none .NET library! Boy, have I got used to that. There is a libcurl.NET project on SourceForge but it's not been updated for a couple of years and hasn't been downloaded very often so not much faith in that avenue – Rob Nicholson Sep 09 '15 at 22:09
  • Although I think I'd prefer to debug the Curl as documented in that link and then re-write in pure .NET – Rob Nicholson Sep 09 '15 at 22:10
  • Kkinsey - can you put your link as an answer so I can up vote it. Read the entire post now and exactly what I'm looking for. Thanks! – Rob Nicholson Sep 09 '15 at 22:17
  • Done! Thanks for the vote of confidence! – Kevin_Kinsey Sep 10 '15 at 13:03

2 Answers2

1

Rob,

Perhaps you should look at this?

Php - Debugging Curl

Best wishes for success :-)

Community
  • 1
  • 1
Kevin_Kinsey
  • 2,285
  • 1
  • 22
  • 23
0

The link gives the first part of the answer in that you can debug Curl requests using the verbose options and piping of error output.

However, sadly, the verbose option in Curl doesn't show multipart requests in the log so the key bit I was looking for is missing.

But the link did take me down another route of trying to capture the low-level requests. Wireshark is a little low level but another hop brought me to discover the incredibly useful Charles Proxy utility. This is an awesome bit of software if you ever have to debug HTTP post requests.

You have to configure your CURL code manually to use the Charles Proxy using this line:

curl_setopt( $ch, CURLOPT_PROXY, "127.0.0.1:8888");

But once that's configured, you can browse through the request and response in the excellent set of Charles diagnostic windows.

That's saved me hours of guesswork. I can see the POST data in all it's glory.

Rob Nicholson
  • 1,943
  • 5
  • 22
  • 37