2

I am attempting to make an xml post request which expects a response using curl to build the headers and content. Here is my code:

<?php
function post_xml($url, $xml) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/xml; charset=utf-8"));
  curl_setopt($ch, CURLOPT_USERPWD, 'myusername:mypassword');
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

  $result = curl_exec($ch);
  $info = curl_getinfo($ch);
  curl_close($ch);
  return $result;
}

$xml = '<?xml version="1.0" encoding="UTF-8"?><testing><test type="integer">1</test></testing>';

$url = "http://example.com/api/test.php";
$result = post_xml($url, $xml);
echo "<pre>"; print_r($result); echo "</pre>";
?>

To view my outgoing request I use:

$info = curl_getinfo($ch);
echo($info['request_header']);

The resulting HTTP request that is sent is:

POST /api/test.php HTTP/1.1
Authorization: Basic pwmtJdCVwNEawdaODH
Host: example.com
Accept: */*
Content-Type: application/xml; charset=utf-8
Content-Length: 95

The content-length is 95, but then it shows none of the xml content below. This results in a 422 Unprocessable Entity error. Am I completely missing something? I expected to be able to see the XML in the request sent.

5
  • The issue must be with the remote API; your code is definitely moving data around. I tested it with a script that would just dump the raw postbody to a test file and return the hash of the request and it worked just fine. Commented Jun 24, 2011 at 15:26
  • WAIT! Are you viewing this in a browser? It might be hiding the XML from you! Try this instead of print_r: echo '<pre>', htmlentities($result), '</pre>'; Commented Jun 24, 2011 at 15:27
  • Sorry for not clarifying, the request I posted is the raw request. I think it may be an issue with the API that I'm trying to use, so I will get in touch with them. This has been driving me nuts all morning, but I'm exactly following their documentation. Commented Jun 24, 2011 at 15:29
  • snippets.dzone.com/posts/show/2596 Commented Jun 24, 2011 at 15:30
  • Your function worked perfectly for me without me changing anything. Thank you very much! Commented Oct 24, 2013 at 14:07

3 Answers 3

2

You haven't specified a fieldname for your XML content, so PHP has nowhere to assign it in the POST request. As the manual states:

This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value.

So try:

curl_setopt($ch, CURLOPT_POSTFIELDS, array('xml' => $xml));

Don't know if this is what your API is expecting, but it's one thing to look into.

As for displaying the XML, remember that browsers can/will interpret it as HTML, and any unknown tags will simply not be displayed. Try doing your output as:

echo "<pre>";
echo htmlspecialchars($result);
echo "</pre>";

and/or outputting header('Content-type: text/plain') so that the XML tags will be displayed as-is and not be interpreted as unknown html tags.

Sign up to request clarification or add additional context in comments.

Comments

0
echo($info['request_header']);

will only show the header that was sent. It will not show the post data, so you should not see the XML string.

1 Comment

Is there any way to easily view the entire message sent?
0

It works when I removed the header line:

//curl_setopt($ch, CURLOPT_HTTPHEADER, ...

Final code:

curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => $xml));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.