Jump to content

Sending image file and xml with curl


Jfergy

Recommended Posts

I have the following code which works just fine sending the $xml. However when i try to add a file to be sent with the xml as well i get an error. I tried using a postData array but then got an array to string error. The way i have it now works but it only sends the xml not the file. Any ideas on how I can send the @file with the postfields? Any help is appreciated
 
$header = array('Content-Type: multipart/form-data');
$xml['xml'] = '<UploadPhoto>';
$xml['xml'] .= '<ID>12345</ID>';
$xml['xml'] .= '<PhotoID>myphoto</PhotoID>';
$xml['xml'] .= '<Filename>myphoto.jpg</Filename>';
$xml['xml'] .= '<Instructions>Need cheeks less rosy</Instructions>';
$xml['xml'] .= '</UploadPhoto>';

$postData = array(
'file' => '@/myphoto.jpg',
'xml'  => $xml
);
}
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, "http://www.myurl.com/api");
curl_setopt($connection, CURLOPT_HTTPHEADER, $header);
curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($connection, CURLOPT_POST, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, $xml);
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
set_time_limit(108000);

$output = curl_exec($connection);
curl_close($connection);
print_r($output);

 

Link to comment
Share on other sites

What does $output say, then?

 

Who controls "myurl.com/api" ... and if it's you, does it produce output ... log files ... error messages?

 

What does the API look like?

 

Pretty hard to guess with not much to look at besides a short script and a description of the issue. It's a two-part system, and there's at *least* a 50-percent chance it's the handler, I'd think.

 

No insult intended ... but these seem to be important questions that you've not told us about yet. :-)

Link to comment
Share on other sites

The API is controlled by another party and output is not given other than to say the file is not sent. In sending the XML its posting properly by itself. The real issue for me is combining the xml post data with an actual file as well. Im sorry I dont have much more than that as the API im working with is not great. However, its good enough to know I did not send it a file when I just send the xml. Output right now simply states no file uploaded with order.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.