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.
Related
I had this line of code which was working fine until I update the php version of my server from 5.3 to 5.6.
It re-writes a xml file.
I guess this line is not compactible with 5.6, but I don't know what is wrong exactly:
$newXml->asXml('my_xml.xml');
And this is the error message:
Fatal error: Call to a member function asXml() on boolean in /usr/home/example/www/sites/all/modules/custom/inmovilla_rewrite/inmovilla_rewrite.module on line 71
What changes should I do here?
UPDATE Complete function
<?php
/**
* Implemetns hook_cron()
*/
function inmovilla_rewrite_cron() {
$xml_external_path = 'http://ap.apinmo.com/portal/mls/jamp623433/my_xml.xml';
$xml = file_get_contents($xml_external_path);
$pattern = '/<unico>(.*?)<\/unico>/';
$response = preg_replace_callback($pattern,function($match){
$valueUnico = trim($match[1]);
$valueUnico = substr($valueUnico, 0, -4);
return '<unico>'.$valueUnico.'</unico>';
},$xml);
$searches = array();
$replacements = array();
for ($i = 1; $i <= 30; $i++) {
$searches[] = 'foto'.$i.'>';
$replacements[] = 'foto1>';
}
$response = str_replace( $searches, $replacements, $response ) ;
$newXml = $response;
$newXml = simplexml_load_string( $newXml );
$newXml->asXml('my_xml.xml');
}
You are calling the method: asXml() on a boolean data-type, not an instance of SimpleXMLElement - you are presumably not feeding in a "well-formed" XML string.
See here: http://php.net/manual/en/function.simplexml-load-string.php
Returns an object of class SimpleXMLElement with properties containing the data held within the xml document, or FALSE on failure.
You should firstly implement some error checking rather than assume that you have a SimpleXMLElement instance, e.g.
$newXml = simplexml_load_string($newXml);
if ($newXml instanceof SimpleXMLElement) {
$xml = $newXml->asXml('my_xml.xml');
}
However, you will need to do some further debugging on the values held in $newXml / $response to see what is wrong with the XML string. I cannot see the XML contents in your question to advise any further as to why the change of PHP version has affected this.
Am dynamically loading an xml file and sending request to the api but getting
Warning: DOMDocument::loadXML(): Empty string supplied as input in /home/spotrech/public_html/ but this error is very inconsistent sometime appear sometime don't! I really no idea how to solve this. below is code
$rechargeApiUrl = "http://allrechargeapi.com/apirecharge.ashx?uid=$uid&apikey=$apike&number=$mobileNo&opcode=$opId&amount=$amount&ukey=$uniId&format=xml";
$url = file_get_contents($rechargeApiUrl);
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML(preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $url));
$itemInfo = $xmlDoc->getElementsByTagName('Result'); //returns an object.
$itemCount = $itemInfo->length;
foreach ($itemInfo as $userInfo) {
//Assigning node values to its specified variables.
$ukey = strtolower($userInfo->getElementsByTagName('ukey')->item(0)->childNodes->item(0)->nodeValue);
$status = $userInfo->getElementsByTagName('status')->item(0)->childNodes->item(0)->nodeValue;
$resultCode = $userInfo->getElementsByTagName('resultcode')->item(0)->childNodes->item(0)->nodeValue;
}
$strStatus = strtolower(trim($status));
$strResultCode = trim($resultCode);
$strCode = trim($ukey);
any response will be appreciated.Thank you
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 am trying to read a weather feed from Yahoo to my site.
Using the code below I was able to print the xml.
What I really want to achieve now is to put the temperature and image in two different variables
$zipCode = "44418";
$url = "http://weather.yahooapis.com/forecastrss";
$zip = "?w=$zipCode";
$fullUrl = $url . $zip.'&u=c';
$curlObject = curl_init();
curl_setopt($curlObject,CURLOPT_URL,$fullUrl);
curl_setopt($curlObject,CURLOPT_HEADER,false);
curl_setopt($curlObject,CURLOPT_RETURNTRANSFER,true);
$returnYahooWeather = curl_exec($curlObject);
curl_close($curlObject);
print "yahooWeather". $returnYahooWeather;
//$temperature
//$image
You should go ahead and use simplexml or DOM to parse the XML and then you can iterate over the results. With SimpleXML this looks like this:
$zipCode = "44418";
$url = "http://weather.yahooapis.com/forecastrss";
$zip = "?w=$zipCode";
$fullUrl = $url . $zip.'&u=c';
$curlObject = curl_init();
curl_setopt($curlObject,CURLOPT_URL,$fullUrl);
curl_setopt($curlObject,CURLOPT_HEADER,false);
curl_setopt($curlObject,CURLOPT_RETURNTRANSFER,true);
$returnYahooWeather = curl_exec($curlObject);
curl_close($curlObject);
//print "here". $returnYahooWeather;
$xmlobj=simplexml_load_string($returnYahooWeather);
$res = $xmlobj->xpath("//yweather:condition");
$tmp = false;
while(list( , $node) = each($res)) {
$tmp = $node;
}
$attribs = $tmp->attributes();
print "Temperature [".$attribs['temp']."]";
I find it easiest to SimpleXML with PHP.
$xml = simplexml_load_string($returnYahooWeather);
echo $xml->Path->To->Temperature;
It's easy enough, and you can use XPath with SimpleXML :). There are other ways of parsing XML too, as previously mentioned DOMDocument is one of them.
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
}