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;
?>