PHP Classes

How to post a XML content

Recommend this page to a friend!

      PHP OAuth Library  >  PHP OAuth Library package blog  >  Learn with a PHP OAut...  >  All threads  >  How to post a XML content  >  (Un) Subscribe thread alerts  
Subject:How to post a XML content
Summary:XML failed to parse when sent using POST
Messages:7
Author:dhaivat naik
Date:2017-02-03 14:07:04
 

  1. How to post a XML content   Reply   Report abuse  
Picture of dhaivat naik dhaivat naik - 2017-02-03 14:07:04
Hi Manuel,

I have implemented this library with many Third Party apps like Google,Twitter,Instagram. It went smooth. Currently i came across a Third Party Application which requires XML content to be sent using POST.

I am not sure how would i achieve this. Tried many variation but dint succeed.

$path = "https://example.com/api/test.xml";
$xml = "<?xml version='1.0' encoding='UTF-8'?><testnode>Hello World!</testnode>";*/
$options = array();
$options['FailOnAccessError'] = true;
//$options['RequestBody'] = $xml;
//$options['RequestContentType'] = 'application/xml;charset=utf-8';
//$options['RequestHeaders'] = array('application/xml');
$aResponse = $client->CallAPI($path,'POST',$params,$options);


The api call goes to the Application but in return it says,
XML failed to parse. Please verify your XML body contains valid XML and that your Content-Type header is set to application/xml.

I tried playing with the 'option' argument of CallAPI method, but there is definitely something which i am missing.

  2. Re: How to post a XML content   Reply   Report abuse  
Picture of dhaivat naik dhaivat naik - 2017-02-03 14:13:19 - In reply to message 1 from dhaivat naik
Sorry for above message, Original goes like this,

$path = "https://example.com/api/test.xml";
$xml = "<?xml version='1.0' encoding='UTF-8'?><testnode>Hello World!</testnode>";

$params['xml']=$xml;

$options = array();
$options['FailOnAccessError'] = true;
//$options['RequestBody'] = $xml;
//$options['RequestContentType'] = 'application/xml;charset=utf-8';
//$options['RequestHeaders'] = array('application/xml');
$aResponse = $client->CallAPI($path,'POST',$params,$options);


The api call goes to the Application but in return it says,
XML failed to parse. Please verify your XML body contains valid XML and that your Content-Type header is set to application/xml.

I tried playing with the 'option' argument of CallAPI method, but there is definitely something which i am missing.

  3. Re: How to post a XML content   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2017-02-03 21:53:38 - In reply to message 1 from dhaivat naik
Using the RequestBody and RequestContentType parameters is the way to go.

Enable debugging and show the debug output for the request to see what is actually being sent and received from the Web server.

  4. Re: How to post a XML content   Reply   Report abuse  
Picture of dhaivat naik dhaivat naik - 2017-02-10 09:03:39 - In reply to message 3 from Manuel Lemos
I guess the RequestHeaders option : headers override any values set by the class when sending the API call HTTP request.

Because of this, the necessary oAuth access token and other things needed are getting removed from the request and it says "API call: it was returned an unexpected response status 401 Response: Invalid OAuth Request"

Can you tell what oAuth values should be set in RequestHeaders array that it authenticates the call?

  5. Re: How to post a XML content   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2017-02-10 09:09:06 - In reply to message 4 from dhaivat naik
Can you provide minimal sample code that I can use to reproduce the problem?

  6. Re: How to post a XML content   Reply   Report abuse  
Picture of dhaivat naik dhaivat naik - 2017-02-10 10:59:58 - In reply to message 5 from Manuel Lemos
Hats off to you for the amazing class. I got the solution. It took me some time to understand the options, and debugging the Headers and Arguments managed in function SendAPIRequest

Problem statement:
The Application for which I need to send data was to POST the XML in the Body. and Header Content-Type = application/xml;

Solution: Used the options

PostValuesInURI=true;
RequestBody=$xml;
RequestContentType = 'application/xml;';
RequestHeaders = array('application/xml'=>'application/xml');

With the normal

$this->__apiCall($path,'POST',$params);

The oAuth headers and values were going in PostValues as shown below.

OAuth client: DEBUG DHAIVAT Headers Array
(
[Protocol] => https
[HostName] => abc.com
[Headers] => Array
(
[Host] => abc.com
[User-Agent] => PHP-OAuth-API (http://www.phpclasses.org/oauth-api $Revision: 1.153 $)
[Accept] => */*
)

[HostPort] => 0
[RequestURI] => /api/documents.xml
[RequestMethod] => POST
[PostValues] => Array
(
[oauth_consumer_key] => vpnHwXXXXXXXXXXXXXXXXXXXXXXXX
[oauth_nonce] => 3d5967XXXXXXXXXXXXXXXXX
[oauth_signature_method] => HMAC-SHA1
[oauth_timestamp] => 1486720763
[oauth_version] => 1.0
[oauth_token] => SqgxzXXXXXXXXXXXXXXXXXXXX
[xml] => MYXMLCONTENT
[oauth_signature] => BlIXXXXXXXXXXXXX
)
)

The API method required to send the XML as POST. So i tried with various option like

$options['RequestBody'] = $xml;
$options['RequestContentType'] = 'application/xml;';
$options['RequestHeaders'] = array('application/xml'=>'application/xml');
$this->__apiCall($path,'POST',$params,$options);

This use to replace the 'PostValues' with 'Body' and because of that all the oAuth necessary authorization strings was not sent to the application, and it use to tell invalid oAuth request.

OAuth client: DEBUG DHAIVAT Headers Array
(
[Protocol] => https
[HostName] => abc.com
[Headers] => Array
(
[Host] => abc.com
[User-Agent] => PHP-OAuth-API (http://www.phpclasses.org/oauth-api $Revision: 1.153 $)
[Content-Type] => application/xml;
[Accept] => */*
)

[HostPort] => 0
[RequestURI] => /api/documents.xml
[RequestMethod] => POST
[Body] => MYXMLCONTENT
)

Using PostValuesInURI=true made my day. It moved all the oAuth strings to the 'RequestURI' and the 'Body' had only the xml content which was to be sent. And it worked.

OAuth client: BEFORE DHAIVAT HeadersArray
(
[Protocol] => https
[HostName] => abc.com
[Headers] => Array
(
[Host] => abc.com
[User-Agent] => PHP-OAuth-API (http://www.phpclasses.org/oauth-api $Revision: 1.153 $)
[Content-Type] => application/xml;
[Accept] => */*
)

[HostPort] => 0
[RequestURI] => /api/documents.xml?oauth_consumer_key=vpnHwXXXXXXXXXXXXXXXXXXXXXXXX&oauth_nonce=3d5967XXXXXXXXXXXXXXXXX&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1486722246&oauth_version=1.0&oauth_token=SqgxzXXXXXXXXXXXXXXXXXXXX&oauth_signature=BlIXXXXXXXXXXXXX
[RequestMethod] => POST
[Body] => MYXMLCONTENT
)

once again thanks for building this class, and documenting it well.

  7. Re: How to post a XML content   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2017-02-14 05:31:48 - In reply to message 6 from dhaivat naik
You just need to Set RequestBody and RequestContentType. Do not sent RequestHeaders as it overrides the request headers.

Take a look at the login_with_infusionsoft.php example script.

phpclasses.org/package/7700-PHP-Aut ...