Warning while load xml using the DOMDocument - php

I used the following code to load the xml for further process, while load itself it display the following warning in the client server but its working fine in my local machine.
Code:
$xmlDoc = new DOMDocument();
$xmlDoc->load('http://www.domainname.com/xmlfilename');
Warning: DOMDocument::load(http://www.domainname.com/xmlfilename)
[domdocument.load]: failed to open stream: Connection timed out
Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed
to load external entity "http://www.domainname.com/xmlfilename"

Increase the time until Connection Timeout before calling load:
libxml_set_streams_context(
stream_context_create(
array('http' => array('timeout' => 120))
)
);
or
ini_set('default_socket_timeout', 120);
See
libxml_set_streams_context — Set the streams context for the next libxml document load or write
HTTP Context Options
Ini Setting: default_socket_timeout

First, make sure your client's server is configured with allow_url_fopen set to true.
I'd also be checking firewall rules on the server. Perhaps it's not allowed to make HTTP requests.

Related

A PHP program access a website timout

My PHP program access a website which is very slow to open, therefore I get a warning message:
Warning: file_get_contents(http://www.example.com): failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\xampp\htdocs\mezi.php on line 155.
Line 155 code: $html = file_get_contents('http://www.example.com');
My question is: how to increase time allowed to wait for slow website? I have already increased allowed execution time by adding set_time_limit(100); to my code. But this does not help.
This is actually a socket session that's being created behind the scenes, therefore the correct value in php.ini would actually be
default_socket_timeout

SoapClient: Unable to find the wrapper "https"

I am having serious trouble trying to get this url to load in my code. When I go to the actual page in the browser it loads fine, but when I parse it its basically giving a 404, I have tried this using soap client and curl thinking ok maybe im doing something wrong.
This is my code, you can see its the bare basics.
$customerId = 'xxxx';
$authenticationId = 'xxxx';
$url = 'https://test.api.800loanmart.com/LoanmartService.svc?wsdl';
$config = array('trace' => 1, 'exceptions' => 0);
$service = new SoapClient($url,$config);
$result = $service->GetTermsAndConditions($customerId, $authenticationId);
var_dump($service);
and this is what it is throwing back at me...
Warning: SoapClient::SoapClient() [soapclient.soapclient]: Unable to find the wrapper "https" - did you forget to enable it when you configured PHP? in C:\xampp\htdocs\loanmart\index.php on line 6
Warning: SoapClient::SoapClient() [soapclient.soapclient]: I/O warning : failed to load external entity "https://test.api.800loanmart.com/LoanmartService.svc?wsdl" in C:\xampp\htdocs\loanmart\index.php on line 6
Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'https://test.api.800loanmart.com/LoanmartService.svc?wsdl' : failed to load external entity "https://test.api.800loanmart.com/LoanmartService.svc?wsdl" in C:\xampp\htdocs\loanmart\index.php on line 6
I have checked to see if I have openssl and it is turned on in my php.ini file, im just pretty stumped as to what to do.
Simple issue that gave me headaches! Open your php.ini file in your apache server directory and uncomment(enable) the line extension=php_openssl.dll.
This fix my issue.
Even if you did check, this is nothing but an openssl configuration issue (used in SoapClient, but still)
Try to file_get_contents($wsdl_url);
Also, check your currently loaded php.ini & supported streams (php.ini might differs from CLI to web SAPI)
php -r "phpinfo();" > file.txt
Mine :
Registered PHP Streams => php, file, glob, data, http, ftp, zip, compress.zlib, compress.bzip2, https, ftps, phar
Registered Stream Socket Transports => tcp, udp, ssl, sslv3, sslv2, tls
(your code sample works on my computer :p)
make active extension=php_openssl.dll in php.ini. Restart server.

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O
warning : failed to load external entity
"USD_en_productdata/USD_en_productdata.xml"
the code
$src=simplexml_load_file("USD_en_productdata/USD_en_productdata.xml");
foreach($src->ProductItem as $i){
}
Try to pass full directory path if you are trying to load xmls held at your server
simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/uproYourFoldet/USD_en_productdat/USD_en_productdata.xml')
or if you want to access xml by http protocol you will need to set allow_url_fopen ON in php.ini or
ini_set('allow_url_fopen ','ON');
in your code. or you can also do this if you are using php version <5
$temp = file_get_contents($url);
$XmlObj = simplexml_load_string($temp);
Looks like your path might not be correct.
In my experience, it's best to surround simplexml_load_file and subsequent functions with if statements with file_exists(...) as the condition.
So in your case:
if(file_exists($_SERVER['DOCUMENT_ROOT'].'/yourPathToFile/...')) {
simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/urltoYourFolder/USD_en_productdat/USD_en_productdata.xml')
}
EDIT: spelling

Identifying root cause of a timeout in PHP

A little while ago I noticed some Soap errors emitting from my app and I started to investigate them. Stuff like:
SoapClient::SoapClient(http://###.###.###.###:8080/path/to/some.wsdl): failed to open stream: HTTP request failed!
SoapClient::SoapClient(): I/O warning : failed to load external entity "http://###.###.###.###:8080/path/to/some.wsdl"
SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://###.###.###.###:8080/path/to/some.wsdl' : failed to load external entity "http://###.###.###.###:8080/path/to/some.wsdl"
It looked like a timeout on the remote server (WSDL caching was turned off). After bouncing that server and having no luck, I tried to just file_get-contents() the WSDL to see what would happen...
No dice: After about 20 seconds or so I got the same stream error:
file_get_contents(http://###.###.###.###:8080/path/to/some.wsdl) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: HTTP request failed!
In a last ditch effort, I tried to read the contents via the curl_* functions, and I do in fact get what I'm looking for.
... tl;dr?
SoapClient and file_get_contents appear to be timing out (though not an explicit "Failed to open stream, connection timed out")
It appears to be related to streams since curl gives me what I'm looking for.
I've got a lot of code that depends on SoapClient and file_get_contents so switching to an all curl solution isn't really an option.
This is not a DNS issue as I can resolve external names fine (and my target resource is an IP)
allow_url_fopen is enabled.
Any ideas?
allow_url_fopen needs to be On in your PHP settings for file_get_contents to work with URLs. It'll give you that exact error otherwise. Double-check your PHP settings by loading a page with a phpinfo(); call to make sure they're not being overridden by a different php.ini or .htaccess file.
I'd guess a firewall problem otherwise but you say curl works from inside PHP, which would be opening sockets in the same manner.

file_get_contents() error

I am using file_get_contents on my PHP and it throws some errors:
My code
#try to fetch from remote
$this->remotePath = "http://some-hostname.com/blah/blah.xml
$fileIn = #file_get_contents($this->remotePath);
The errors:
Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /virtual/path/to/file/outputFile.php on line 127
Warning: file_get_contents(https://some-host-name/data/inputFile.xml) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in /virtual/path/to/file/outputFile.php on line 127
Any idea? It worked fine in my computer but stopped working when I ported it to the web server.
Your server must have the allow_url_fopen property set to true. Being on a free webhost explains it, as it's usually disabled to prevent abuse. If you paid for your hosting, get in contact with your host so they can enable it for you.
If changing that setting is not an option, then have a look at the cURL library.
It seems "allow_url_fopen" setting is false on your server and hence does not allow using URLs with file_get_contents().
Try using CURL instead that is a better and efficient way of communicating with other server.

Categories