I am using SOAP to get data from some server (data coming in .xml) . But sometimes SOAP server is down and I need to display some error message instead of :
Warning: simplexml_load_string(): Entity: line 2: parser error : Start tag expected, '<' not found in /var/www/class/data2.php on line 619 Warning: simplexml_load_string(): in /var/www/class/data2.php on line 619 Warning: simplexml_load_string(): ^ in /var/www/class/data2.php
My code is:
$client = new SOAPClient ( 'link.wsdl' ); // initiate new SoapClient
$password ['_'] = 'PASSWORD'; // password for authenticate_user function in SoapHeader
$encoded = new SoapVar ( $password, SOAP_ENC_OBJECT ); // make SoapVariable out of $password
$header = new SoapHeader ( 'http://soapinterop.org/echoheader/', 'authenticate_user', $encoded ); // put authenticate_user method, and password in header
$client->__setSoapHeaders ( $header ); // set SoapHeader
$response = $client->get_details ($this->vin); // calling get_details with the vin given in the form field
$xml = simplexml_load_string ( $response ); // converting the response string to xml
$json = json_encode ( $xml ); // converting to an array in to easy steps (step 1)
$array = json_decode ( $json, TRUE ); // step 2
What I want:
Replace Warning message with something like: "This service is temporary unavaliable"
After
$xml = simplexml_load_string ( $response );
check if
$xml === false
and set the error message accordingly
http://php.net/manual/en/function.simplexml-load-string.php
As you can read in manual the simplexml_load_string function:
Returns an object of class SimpleXMLElement with properties containing the data held within the xml document, or FALSE on failure.
So simply check if it fails and if it does echo your This service is temporary unavaliable
To get rid of this warning consider to ini_set('display_errors', '0'); on production
Related
I have the following url:
https://blockchain.info/multiaddr?active=1AT4ES3ee1N6iBzzbdK8xvcAV3CBTRKcbS|1FHcYth4LRJMwNx2y8NR5DH7sYCiVzXs3Y&n=1
I want to access the final_balance from the output of the url.
I have the following code:
$value = file_get_contents($url);
$FinalBalance = $value["final_balance"];
var_dump($FinalBalance);
Error PHP Warning: Illegal string offset 'final_balance'
I also tried the following code:
$value = file_get_contents($url);
$json = json_decode($value);
var_dump($json);
$FinalBalance = $json["final_balance"];
var_dump($Final_Balance);
Error PHP Fatal error: Uncaught Error: Cannot use object of type stdClass as array
Your are near to finish that stuff but I will write down desired solution. Please have a look.
$url="https://blockchain.info/multiaddr?active=1AT4ES3ee1N6iBzzbdK8xvcAV3CBTRKcbS|1FHcYth4LRJMwNx2y8NR5DH7sYCiVzXs3Y&n=1";
$value = file_get_contents($url);
$FinalBalance = $value;
$data=json_decode($FinalBalance);
echo $data->wallet->final_balance;
echo $data->addresses[0]->final_balance;
echo $data->addresses[1]->final_balance;
exit;
You are going to access the inner object so you have to provide proper reference whether it is an array or an object.
I have a bit of code that takes in XML and attempts to validate it. However, I keep getting an error
Error type: 8192
Error message: Assigning the return value of new by reference is deprecated
From looking around it seems like there must be underlying code that uses assignment by reference e.g $foo = & ... I tried to change my code e.g. to use the technique used here but to no avail.
$version = $_POST["xacmlversion"];
$source = $_POST["policy"];
if (get_magic_quotes_gpc()){
$source = stripslashes($source);
}
$xdoc = new DomDocument();
if ($version == 2){
$xmlschema = 'xacml/xacml2.xsd';
} else {
$xmlschema = 'xacml/xacml3.xsd';
$version = 3;
}
$xdoc->LoadXML($source);
//Validate the XML file against the schema
$valid = $xdoc->schemaValidate($xmlschema);
So where's the blooper?
UPDATE
Here is the root cause
Array
(
[type] => 8192
[message] => Assigning the return value of new by reference is deprecated
[file] => /home/www/wp-content/plugins/exec-php/includes/ajax.php
[line] => 64
)
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.
I have this code :
$feed = 'urltorss';
$feeds = new SimpleXmlElement( file_get_contents($feed) );
$feed = 'http://www.businessweek.com/feeds/homepage.rss';
$feeds = new SimpleXmlElement( file_get_contents($feed) );
Now both urls don't have any special characters that require url encoding them, but the 2nd url has an .rss extension if it's related.
The first call works and i get a timeout on the 2nd when i try on my server but works perfectly on localhost.
This is the error i'm getting :
Severity: Warning
Message: file_get_contents('http://www.businessweek.com/feeds/homepage.rss')
[function.file-get-contents]:
failed to open stream: Connection timed out
Filename: controllers/mailsystem.php
Line Number: 36
Why is that?
First, check your path so
echo $feed; // just for debug
then check the content
$content = #file_get_contents($feed);
and in the end, get the XML
if( $content ) { $feeds = new SimpleXmlElement( file_get_contents($feed) ); }
Edit: file_get_contents for URLs will only work if you have allow_url_fopen = 1. To get the content of http://www.businessweek.com/feeds/homepage.rss you need a cURL function that gets the content. Something like http://davidwalsh.name/curl-download
Using my webservice I would like to save in a XML file the response that it display to me, but when I'm trying to save this data it displays mze an error:
here is the error I get:
Catchable fatal error: Object of class stdClass could not be converted to string in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\DOSSIERS\creditsafe.php on line 13
the code I'm using is there; but I don't know I know that the response is as XML, but id doesn't save in the file I wanted I don't know why, just because it is not a varchar.
<?php
$wsdl = "https://www.creditsafe.fr/getdata/service/CSFRServices.asmx?WSDL";
$client = new SoapClient($wsdl);
$debiteur_siret = "<xmlrequest><header><username>demo</username><password>**********</password><operation>getcompanyinformation</operation><language>FR</language><country>FR</country><chargereference></chargereference></header><body><package>standard</package><companynumber>40859907400049</companynumber></body>
</xmlrequest> " ;
$o = new stdClass();
$o->requestXmlStr = $debiteur_siret;
$fichier =
//header('Content-Type: text/xml');
$texte=$client->GetData($o);
echo $texte;
$fp = fopen("tmp/".$_GET['n_doss'].".xml", "w+");
//fwrite($fp, $texte);
fclose($fp);
?>
The message comes from the lines:
$texte=$client->GetData($o);
echo $texte;
GetData does not return a string, but a stdClass instead, which can not be converted to a string. var_dump($texte) to see what it returns, and echo the appropriate property of the stdClass.
EDIT: I've looked up the WDSL and checked; the GetData() function returns a GetDataResponse, which seems to contain a property GetDataResult (a string). So the following should work:
$texte=$client->GetData($o);
echo $texte->GetDataResult;