I have the following cUrl function call that is returning the response code.
$result = curl_exec($ch);
I need to check the $result for the following response property.
<isValidPost>true</isValidPost>
I could always parse through and check for that exact string, but is there an alternative way to check this?
Presuming that the API is set in stone (i.e. won't change at a moment's notice), in this case it's probably safe to a simple string search
if (false !== strpos($result, '<isValidPost>true</isValidPost>')) {
However, to do this in a more reliable way, you may want to do DOM parsing:
$dom = new DOMDocument;
$dom->parseXML($result);
$isValidPostTag = $dom->getElementsByTagName('isValidPost')->item(0);
if ($isValidPostTag) {
$isValidPost = $isValidPostTag->nodeValue == 'true';
} else {
$isValidPost = false;
}
Are you getting XML response? Then parse the XML
$xml = new SimpleXMLElement($result);
if(isset($xml->your_element->isValidPost)){
//do what you need
}
Related
I have a C# client, its send a json to my php server.
There is the json string:
{"data":[{"name":"1"}]}
Its a valid json. When i try it in PHP Sandbox its works good
$ad = '{"data":[{"name":"1"}]}';
$contents = utf8_encode($ad );
$results = json_decode($contents);
var_dump($results->data);
But,when i try in laravel 5.1, its not work good.
$response = $connection -> getData();
// return $response; (Its equal : {"data":[{"name":"1"}]} )
$contents = utf8_encode($response);
$results = json_decode($contents);
dd($results->data); // Error Trying to get property of non-object
I hope someone can help me.
Thanks!
Based on the comments, it looks like the socket_read() in the getData() method is reading 1 character at a time, and then concatenating each NUL terminated character into a response string. json_decoded() is choking on all the extra NUL characters.
You can either update your logic in the getData() method so it is not doing this, or you can run a str_replace on the results:
$response = $connection -> getData();
// get rid of the extra NULs
$response = str_replace(chr(0), '', $response);
$contents = utf8_encode($response);
$results = json_decode($contents);
dd($results->data);
I have a SQL query that returns a XML string. It works. However, I just moved to a windows php server(from a linux one) and I can no longer get the xml string from the database. It just comes out as one big string(not in xml format) so I cannot parse through all the data.
$query = "SELECT s FROM returnXML('id') FOR XML AUTO, TYPE;";
$stmt = sqlsrv_query($conn, $query);
if(sqlsrv_fetch($stmt) !== false) {
$xml = sqlsrv_get_field($stmt, 0);
echo stream_get_contents($xml);
var_dump($xml);
}
Any help would be appreciated. With the current code. It prints the data(all crossed out though..) and then 'resource(5) of type (stream)'
Thanks!
Replaced code with
if (sqlsrv_fetch($stmt) !== false) {
$xml_res = sqlsrv_get_field($stmt, 0);
$xml_string = stream_get_contents($xml_res);
$xml = simplexml_load_string($xml_string);
}
$xml is now a php variable that has the xml data in it as type SimpleXMLElement.
I'm trying to encode a json string but it keeps returning null. I've tried a few suggestion here on st
$url = "https://www.google.com/finance?output=json&start=0&num=200&noIL=1&q=[currency%20%3D%3D%20%22USD%22%20%26%20%28%28exchange%20%3D%3D%20%22NYSEMKT%22%29%20%7C%20%28exchange%20%3D%3D%20%22NYSEARCA%22%29%20%7C%20%28exchange%20%3D%3D%20%22NYSE%22%29%20%7C%20%28exchange%20%3D%3D%20%22NASDAQ%22%29%29%20%26%20%28change_today_percent%20%3E%3D%20-101%29%20%26%20%28change_today_percent%20%3C%3D%20-3%29%20%26%20%28volume%20%3E%3D%20150001%29%20%26%20%28volume%20%3C%3D%20313940000%29%20%26%20%28last_price%20%3E%3D%2010%29%20%26%20%28last_price%20%3C%3D%20229301%29]&restype=company&ei=T5mTVKG5IYT1igKEoYHQCQ";
$obj = file_get_contents($url);
$obj = json_decode($obj);
var_dump($obj);
JSON doesn't support \x escapes, so it's invalid JSON by definition, hence why json_decode() returns null. Decoding the valid JSON \u0022 works fine.
$url = "https://www.google.com/finance?output=json&start=0&num=200&noIL=1&q=[currency%20%3D%3D%20%22USD%22%20%26%20%28%28exchange%20%3D%3D%20%22NYSEMKT%22%29%20%7C%20%28exchange%20%3D%3D%20%22NYSEARCA%22%29%20%7C%20%28exchange%20%3D%3D%20%22NYSE%22%29%20%7C%20%28exchange%20%3D%3D%20%22NASDAQ%22%29%29%20%26%20%28change_today_percent%20%3E%3D%20-101%29%20%26%20%28change_today_percent%20%3C%3D%20-3%29%20%26%20%28volume%20%3E%3D%20150001%29%20%26%20%28volume%20%3C%3D%20313940000%29%20%26%20%28last_price%20%3E%3D%2010%29%20%26%20%28last_price%20%3C%3D%20229301%29]&restype=company&ei=T5mTVKG5IYT1igKEoYHQCQ";
$obj = file_get_contents($url);
$obj = trim($obj);
$obj = str_replace('\x', '\u00', $obj);
$obj = json_decode($obj,true);
var_dump($obj);
Try this code
<?php
function convert($string) {
return preg_replace_callback('#\\\\x([[:xdigit:]]{2})#ism', function($matches) {
return htmlentities(chr(hexdec($matches[1])));
}, $string);
}
$url = "https://www.google.com/finance?output=json&start=0&num=200&noIL=1&q=[currency%20%3D%3D%20%22USD%22%20%26%20%28%28exchange%20%3D%3D%20%22NYSEMKT%22%29%20%7C%20%28exchange%20%3D%3D%20%22NYSEARCA%22%29%20%7C%20%28exchange%20%3D%3D%20%22NYSE%22%29%20%7C%20%28exchange%20%3D%3D%20%22NASDAQ%22%29%29%20%26%20%28change_today_percent%20%3E%3D%20-101%29%20%26%20%28change_today_percent%20%3C%3D%20-3%29%20%26%20%28volume%20%3E%3D%20150001%29%20%26%20%28volume%20%3C%3D%20313940000%29%20%26%20%28last_price%20%3E%3D%2010%29%20%26%20%28last_price%20%3C%3D%20229301%29]&restype=company&ei=T5mTVKG5IYT1igKEoYHQCQ";
$obj = file_get_contents($url);
$obj = convert($obj);
$obj = json_decode($obj);
echo '<pre>'; print_r($obj); echo '</pre>';
?>
As recommended here (and pointed out by #showdev already, thanks!) you should use their RSS API and parse that as XML instead, because Google does not deem it necessary to honour web standards (here, they fail at JSON; elsewhere, they fail at simple things like SMTP and especially IMAP).
JSON has a very detailed specification, and it is very easy to adhere to in generators. Making JSON parsing strict is recommended for security reasons – especially with external input. So please use Google’s RSS variant.
I have generated some XML which is saved to a file.
Dummy_Order.xml
In the following code I wanted to open the XML and use it in the method 'ImpOrders'
Here is my code
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$proxy = new SoapClient('http://soapclient/wsdl/Web?wsdl', array ('trace' => 1));
if (file_exists('Dummy_Order.xml')) {
$xml = file_get_contents('Dummy_Order.xml');
} else {
exit('Failed to open XML.');
}
$xmlstring = new SimpleXMLElement($xml);
$result = $proxy->ImportOrders($xmlstring);
var_dump($result);
echo "REQUEST:\n" . $proxy->__getLastRequest() . "\n";
?>
I am getting a response in $result of 0 imported 0 skipped. So i then did getLastRequest() and it's adding the code from the Method but not adding my XML. It wants my XML in a string - which it current is in and isnt moaning about that (It does moan if i use it ->asXML).
I have tried
$result = $proxy->ImportOrders();
and
$result = $proxy->ImportOrders($xmlstring);
And both show the same result in _getLastRequest, which led me to believe that my string isn't being plugged in.
When I check the functions using _getFunctions it provides the information of this...
ImportResult ImportOrders(string $Orders)
Any help would be awesome!
Ok so i resolved it. All i needed to do was wrap my answer in <<
Like below.
$teststring = <<<XML
$xml
XML;
$result = $proxy->ImportOrders($teststring);
Hope this helps out anyone else using PHP, SoapClient and XML.
I've ran into trouble with SOAP, I've never had this issue before and can't find any information on line that helps me solve it.
The following code
$wsdl = "path/to/my/wsdl";
$client = new SoapClient($wsdl, array('trace' => true));
//$$textinput is passed in and is a very large string with rows in <item></item> tags
$soapInput = new SoapVar($textinput, XSD_ANYXML);
$res = $client->dataprofilingservice(array("contents" => $soapInput));
$response = $client->__getLastResponse();
var_dump($res);//outputs null
var_dump($response);//provides the proper response as I would expect.
I've tried passing params into the SoapClient constructor to define soap version but that didnt' help. I've also tried it with the trace param set to false and not present which as expected made $response null but $res was still null. I've tried the code on both a linux and windows install running Apache.
The function definition in the WSDL is (xxxx is for security reasons)
<portType name="xxxxServiceSoap">
<operation name="dataprofilingservice">
<input message="tns:dataprofilingserviceSoapIn"/>
<output message="tns:dataprofilingserviceSoapOut"/>
</operation>
</portType>
I have it working using the __getLastResponse() but its annoying me it will not work properly.
I've put together a small testing script, does anyone see any issues here. Do I need a structure for the return type?
//very simplifed dataset that would normally be
//read in from a CSV file of about 1mb
$soapInput = getSoapInput("asdf,qwer\r\nzzxvc,ewrwe\r\n23424,2113");
$wsdl = "path to wsdl";
try {
$client = new SoapClient($wsdl,array('trace' => true,'exceptions' => true));
} catch (SoapFault $fault) {
$error = 1;
var_dump($fault);
}
try {
$res = $client->dataprofilingservice(array("contents" => $soapInput));
$response = $client->__getLastResponse();
echo htmlentities($client->__getLastRequest());//proper request echoed
echo '<hr>';
var_dump($res);//null
echo "<hr>";
echo(htmlentities($response));//proper response echoed
} catch (SoapFault $fault) {
$error = 1;
var_dump($fault);
}
function getSoapInput($input){
$rows = array();
$userInputs = explode("\r\n", $input);
$userInputs = array_filter($userInputs);
//
$inputTemplate = " <contents>%s</contents>";
$rowTemplate = "<Item>%s</Item>";
//
$soapString = "";
foreach ($userInputs as $row) {
// sanitize
$row = htmlspecialchars(addslashes($row));
$xmlStr = sprintf($rowTemplate, $row);
$rows[] = $xmlStr;
}
$textinput = sprintf($inputTemplate, implode(PHP_EOL, $rows));
$soapInput = new SoapVar($textinput, XSD_ANYXML);
return $soapInput;
}
Ok after much digging it is related to relative namespaces, it appears that PHP doesn't handle them well within the WSDL or the SOAP Envelope. So since I don't have control of the SOAP Server I will continue to get the response via __getLastResponse();.
Hopefully this will save some people some time it was hard to find.
You are mixing things here. __getLastResponse() returns the bare XML string response from the server if you make use of the 'trace' => true option. That is for debugging only.
So regardless whether 'trace' => true or not, the method which you would like to call originally returns the same and that is totally normal. Setting tracing to on won't change the behaviour, it just offers an additional feature, the return value for __getLastResponse().
As the SoapClient does not throw any exception I'd say that your call is going okay and NULL is a valid return value.
You might want to provide the actual XML string and/or the WSDL defintion so that one could inspect if that's the case or not.