I have a PHP client that has to query datasets from a service that uses .NET. So, I was trying to make a query by passing a POST field called 'query'. Inside this variable, I have a string version of XML document. What's happening now is that whenever I passed this 'query' to the server, it will throw an exception at me 'Data at the root level is invalid.' and point to the last line of my XML document.
Any tips?
Here's the XML
<?xml version="1.0" encoding="utf-8"?>
<Predicate>
<ComparisonPredicate name="like" isEnabled="True" id="" DistinctIndividualQueries="False">
<FacetOperand reference="TriggeredAlerts.Alert.Name"/>
<Comparison operation="like"/>
<ValueOperand type="System.String" value="Sample"/>
</ComparisonPredicate>
</Predicate>
Here's the var_dump()
string(335) "<?xml version="1.0" encoding="utf-8"?>
<Predicate>
<ComparisonPredicate name="like" isEnabled="True" id="" DistinctIndividualQueries="False">
<FacetOperand reference="TriggeredAlerts.Alert.Name"/>
<Comparison operation="like"/>
<ValueOperand type="System.String" value="TestAlert"/>
</ComparisonPredicate>
</Predicate>
"
I just figured it out and the solution was to use fsocksopen instead of curl. I do not have an accurate answer for that but when I was using curl, it added some extra stuffs to my content; my content length was significantly longer than it is actually supposed to be.
Related
I am trying to setup a Soap connection between a server in php and a client in C. My server is using a working wsdl file and a class to add these methods. I can confirm with Wireshark that my client request is well received and correctly processed.
My issue is that the values of the XML element sent by the server cannot be read because the namespaces differs. By adding debug log in my client I have found that the error is :
Tags 'state' and 'ns2:state' match but namespaces differ
Issue :
The issue seems to be that the server response does not contain any default namespace :
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://www.w3.org/2005/08/adressing"
xmlns:ns2="http://www.namespace1/">
<env:Body>
<ns2:HelloResponse>
<state>OK</state>
<intElement>123</intElement>
</ns2:HelloResponse>
</env:Body>
</env:Envelope>
It looks like <state> and <intElement> are not in any namespace, so it can't match one of the client. In my Wsdl file, these element belongs to xmlns:s="http://www.w3.org/2001/XMLSchema"
What I tried :
Obvious solution is to add an namespace to these element, but I can't find a way to do it.
In my php server, I can modify any request that comes in but can't affect any response that comes out (or at least i didn't find how to do it).
2nd solution : Adding the namespace that describe these element to the Namespace struct in my client and then use the set_namespace() function.
But I couldn't manage to put them to work, please keep in mind that I am still new to the XML/Soap world, any help is appreciated.
As said before, the solution was to add the corresponding namespace to these element.
I managed to do it using ob_get_contents() and adding the namespace to <ns2:HelloResponse>. Using Wireshark was really useful for this kind of stuff.
I have a problem with an EPP XML request. I have been getting a Command Syntax Error response from the API for quite some time now. I've been in contact with the technical department who just recently responded that my XML is being received as malformed, being split in the middle. I don't know why.
I compile the PHP to XML using the DOMDocument class. I return every XML build method using __toString() which returns $this->saveXML(); which dumps the tree as string. I then send a POST request. Every other method works fine, but not this.
Code here:
https://github.com/OssiPesonen/epp_depo
I actually dump my XML using:
header('Content-type: text/plain');
echo $xml;
die();
And every time it gives me:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
<command>
<create>
<contact:create xmlns:contact="urn:ietf:params:xml:ns:contact-1.0">
<contact:id>1</contact:id>
<contact:role>5</contact:role>
<contact:type>1</contact:type>
<contact:postalInfo type="loc">
<contact:isfinnish>1</contact:isfinnish>
<contact:firstname>Essi</contact:firstname>
<contact:lastname>Esimerkki Oy</contact:lastname>
<contact:name>HR</contact:name>
<contact:org>Esimerkki Oy</contact:org>
<contact:birthDate>2005-04-03T00:00:00.0Z</contact:birthDate>
<contact:identity>123423A123F</contact:identity>
<contact:registernumber>20824760</contact:registernumber>
<contact:addr>
<contact:street>Test Street</contact:street>
<contact:pc>00000</contact:pc>
<contact:cc>FI</contact:cc>
<contact:city>City</contact:city>
</contact:addr>
</contact:postalInfo>
<contact:voice x="1234">+358401234567</contact:voice>
<contact:email>testi#testi.fi</contact:email>
<contact:legalemail>testi#testi.fi</contact:legalemail>
<contact:disclose flag="0">
<contact:addr/>
<contact:email/>
</contact:disclose>
</contact:create>
</create>
<clTRID>E1144-588371f25ab80</clTRID>
</command>
</epp>
This doesn't look malformed, right? Yet, the technical support responded to me, that after <contact:city> was split before the closing tag:
contact:city>
<contact:sp>VA</contact:sp>
<contact:pc>20166-6503</contact:pc>
<contact:cc>US</contact:cc>
</contact:addr>
</contact:postalInfo>
<contact:voice x="1234">+3581231234</contact:voice>
<contact:fax>+04040as</contact:fax>
<contact:email>jdoe#example.com</contact:email>
<contact:legalemail>jdoe#example.com</contact:legalemail>
<contact:disclose flag="0">
<contact:addr/>
<contact:email/>
</contact:disclose>
</contact:create>
</create>
<clTRID>E1144-5867f93f6503a</clTRID>
</command>
</epp>
05-04-03T22:00:00.0Z</contact:birthDate>
<contact:identity>010188-123A</contact:identity>
<contact:registernumber>123456-7</contact:registernumber>
<contact:addr>
<contact:street>123 Example Dr.</contact:street>
<contact:street></contact:street>
<contact:street></contact:street>
<contact:city>Dulles</
Any ideas what might cause this? Because I can't see it.
Edit
After exhausting my options I've uploaded the code to Github to be viewed.
https://github.com/OssiPesonen/epp_depo
Well, the answer was simple, yet I cannot understand why it's the solution.
I basically removed all the failsafes from the send() method and leaving just this:
private function send($buffer)
{
if(fwrite($this->socket, $buffer)) {
return true;
}
return false;
}
I don't really know what it was that cut the XML string in the previous one, but this will do for our platform.
I am trying to call the Pho.to API to edit photos, and every time I try to POST I get the same error. I've double and triple checked my app_id and my key, and I can't figure out what I'm doing wrong. I'm currently using the ARC Chrome Extension to call this api, so I haven't even started coding that part yet, I'm just trying to get a real response back from the api to make sure it even works.
I followed the instructions in their docs as well as I could. Here's the link for reference: http://developers.pho.to/documentation/sending-requests
Here is my api call:
http://opeapi.ws.pho.to/addtask?APP_ID=<my-app-id>&KEY=<my-key>&SIGN_DATA=910ceb5bdb238b9248a34cce8b29ba64d5f239df
And here is the response I get back (don't be deceived by the 200):
Status: 200 OK
<?xml version="1.0" ?>
<image_process_response>
<status>SecurityError</status>
<err_code>614</err_code>
<description>Error in POST parameters: one or more parameters (DATA , SIGN_DATA or APP_ID) are empty</description>
</image_process_response>
Here's the PHP code I use to create the SHA1 for SIGN_DATA:
<?php
echo hash_hmac('SHA1', '<image_process_call><lang>en</lang><image_url order="1">http://www.w3schools.com/html/pic_mountain.jpg</image_url><methods_list><method order="1"><name>desaturation</name></method><method order="2"><name>cartoon</name><params>fill_solid_color=1;target_color=(255,255,255);border_strength=20;border_width=3</params></method></methods_list><result_format>png</result_format><result_size>600</result_size></image_process_call>','<my-key>');
?>
Here is the xml from above, formatted for readability:
<image_process_call>
<lang>en</lang>
<image_url order="1">http://www.w3schools.com/html/pic_mountain.jpg</image_url>
<methods_list>
<method order="1">
<name>desaturation</name>
</method>
<method order="2">
<name>cartoon</name>
<params>fill_solid_color=1;target_color=(255,255,255);border_strength=20;border_width=3</params>
</method>
</methods_list>
<result_format>png</result_format>
<result_size>600</result_size>
</image_process_call>
Any help would be appreciated. Thanks in advance!
So I figured out what was wrong. Here is my detailed solution for anyone else that may run into similar problems with this api (regardless of platform):
Part of the problem (as #u_mulder pointed out) is that DATA needs to be sent up along with SIGNED_DATA so that the SHA1 can be decoded on the other end.
The other piece that fixed my problem was removing <lang>en</lang>. For whatever reason, that was returning Error 613: Invalid SIGN_DATA parameter. English is the default language anyway, so it was unnecessary.
So after fixing those things, here's my final url:
http://opeapi.ws.pho.to/addtask/?app_id=<my-app-id>&key=<my-key>9&sign_data=e456c393d11797c1a2945a85dd49ba2208cc66de&data=%3Cimage_process_call%3E%3Cimage_url+order%3D%221%22%3Ehttp%3A%2F%2Fwww.heroesandheartbreakers.com%2Fimages%2Fstories%2Fblogarticles%2F2016%2FJanuary2016%2FTV-Recap-Arrow-4x11-Olicity-is-home-470.jpg%3C%2Fimage_url%3E%3Cmethods_list%3E%3Cmethod+order%3D%221%22%3E%3Cname%3Ecartoon%3C%2Fname%3E%3Cparams%3Efill_solid_color%3D1%3Btarget_color%3D%28255%2C255%2C255%29%3Bborder_strength%3D20%3Bborder_width%3D1%3C%2Fparams%3E%3C%2Fmethod%3E%3C%2Fmethods_list%3E%3Cresult_format%3Epng%3C%2Fresult_format%3E%3Cresult_size%3E1500%3C%2Fresult_size%3E%3C%2Fimage_process_call%3E
Notice that the url is encoded. This may or may not be necessary, I just encoded it to be safe.
This returns:
<?xml version="1.0" ?>
<image_process_response>
<request_id>010afc13-6bba-44dd-b278-4f3bd1e41946</request_id>
<status>OK</status>
<description />
<err_code>0</err_code>
</image_process_response>
And I can now use request_id to get the url of the edited image:
http://opeapi.ws.pho.to/getresult?request_id=010afc13-6bba-44dd-b278-4f3bd1e41946
Which returns the following xml:
<image_process_response>
<request_id>010afc13-6bba-44dd-b278-4f3bd1e41946</request_id>
<status>OK</status>
<result_url>http://worker-images.ws.pho.to/i1/3BCB160A-691A-458B-9161-67AFA8A9EAA0.png</result_url>
<result_url_alt>http://worker-images.ws.pho.to.s3.amazonaws.com/i1/3BCB160A-691A-458B-9161-67AFA8A9EAA0.png</result_url_alt>
<nowm_image_url>http://worker-images.ws.pho.to/i1/3BCB160A-691A-458B-9161-67AFA8A9EAA0.png</nowm_image_url>
</image_process_response>
So the url of the final edited image is http://worker-images.ws.pho.to/i1/3BCB160A-691A-458B-9161-67AFA8A9EAA0.png (I believe links expire after 24 hours)
And we're done!
If you'd like to check out how I implemented this api in a simple Android app, here's the github link: https://github.com/youravgjoe/ColoringPageGenerator
Before:
After:
For me the error was fixed by (A) making sure to use SHA1 rather than SHA256 (my own fault) and (B) for some reason the sign_data value has to be lowercase.
Remove all new lines(Enter) in XML and try again.
<image_process_call>
<lang>en</lang>
<image_url order="1">http://www.w3schools.com/html/pic_mountain.jpg</image_url>
<methods_list>
<method order="1">
<name>desaturation</name>
</method>
<method order="2">
<name>cartoon</name>
<params>fill_solid_color=1;target_color=(255,255,255);border_strength=20;border_width=3</params>
</method>
</methods_list>
<result_format>png</result_format>
<result_size>600</result_size>
</image_process_call>
To
<image_process_call><lang>en</lang><image_url order="1">http://www.w3schools.com/html/pic_mountain.jpg</image_url><methods_list><method order="1"><name>desaturation</name></method><method order="2"><name>cartoon</name><params>fill_solid_color=1;target_color=(255,255,255);border_strength=20;border_width=3</params></method></methods_list><result_format>png</result_format><result_size>600</result_size></image_process_call>
i am having an issue with parsing an XML file using SimpleXML and PHP.
The XML file in question is provided by a third party and includes a number of child elements (going down multiple levels) within it. I know which elements i require and can see them within the XML file, but i just can't seem to get them to print using PHP.
Example XML feed for test.xml:
<?xml version="1.0" encoding="utf-8"?>
<Element1 xmlns="" release="8.1" environment="Production" lang="en-US">
<Element2>
<Element3>
<Element4>
<Element5>it worked</Element5>
</Element4>
</Element3>
</Element2>
</Element1>
The file only includes one of each attribute so i can be very particular with the request, the code i have so far is below:
$lib=simplexml_load_file("test.xml");
$make=$lib->Element1->Element2->Element3->Element4->Element5;
print $make;
I have tried to look this up before asking, but the only solutions i can see are when the child attributes are unknown or there are multiple results for each request, which is not the case in this instance.
Any help or guidance would be greatly received.
Thanks
In your code above, $lib is Element1. So you just need to drop one of your references. This:
$make=$lib->Element1->Element2->Element3->Element4->Element5;
Should become this:
$make=$lib->Element2->Element3->Element4->Element5;
Also, SimpleXML is an awful awful awful awful interface (considering that "Simple" is in the name and there is mass confusion about how to use it). I would always recommend DOMDocument instead.
I'd strongly recommend using xpath as it will give you more flexibility e.g. Allow you to restrict results based on xml node attributes.
$xml = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?>
<Element1 xmlns="" release="8.1" environment="Production" lang="en-US">
<Element2>
<Element3>
<Element4>
<Element5>it worked</Element5>
</Element4>
</Element3>
</Element2>
</Element1>');
$data=$xml->xpath('/Element1/Element2/Element3/Element4/Element5');
echo (string)$data[0]; //outputs 'it worked'
//this also works
$data=$xml->xpath('//Element5');
echo (string)$data[0]; //outputs 'it worked'
I want to perform an if check on an XML file I have loaded in using simplexml_load_file.
I have accessed the data when the output is as I want it to be, although as it's web services sometimes they may be a response which I need to cater for. The below is the XML I want to be able to check on:
<?xml version="1.0" encoding="utf-8"?>
<ProductSizes />
Can somebody help me out? I am using the below:
$xmlstock = simplexml_load_file($xmlfeed);
Assuming that the xml source looks something like the following when actually populated:
<?xml version="1.0" encoding="utf-8"?>
<ProductSizes>
<ProductSize />
<ProductSize />
<ProductSize />
</ProductSizes>
You can get a count using the count() api call:
$xmlstock = simplexml_load_file($xmlfeed);
$hasProductSizes = ($xmlstock->ProductSize->count() > 0) ? true : false;
More info on the count() API via PHP docs.