simplexml_load_file() Connection timed out trying to parse remote xml - php

I am trying to parse an xml file from this url
http://87.102.127.86:8081/kwiktext/kwiktext.exe?Page=52&compid=33&refnum=ICE2-1713464-220
The ref number expires daily so you may see almost no content by the time you look into it. But this is urelated with the issue.
I am trying to parse it with simplexml_load_file() using
<?php
$xml = 'http://87.102.127.86:8081/kwiktext/kwiktext.exe?Page=52&compid=33&refnum=ICE2-1713464-220';
$xml= simplexml_load_file($xml) or die("Error: Cannot create object");
echo urldecode($xml->hoteldescription); ?>
Unsuccessfully it returns 'Connection timed out'.
But when I try to do the same with a random xml url I found on the net the action completes just fine:
<?php $xml=simplexml_load_file('http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V') or die("Error: Cannot create object");
echo $xml->Time['TimeStamp']?>
Why I am getting the Connection Time Out? The providers of the link said that it is a simple task. If I download the xml source from the url on a local file on my server I can fetch it with no problems. So there must be something with the remote request.
Needless to say that I am a php newbie... especially on server requests.
Thanks!
EDIT
I have also tried file_get_contents() and it didn't work for me. Also the last thing I tried again with no success was this
function load_file_from_url($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_REFERER, 'mydomain');
$str = curl_exec($curl);
curl_close($curl);
return $str;
}
function load_xml_from_url($url) {
return simplexml_load_string(load_file_from_url($url));
}
$xml = load_xml_from_url('http://87.102.127.86:8081/kwiktext/kwiktext.exe?Page=52&compid=33&refnum=ICE2-1713464-220');'
EDIT 2
Apparently the 8081 port that was required from the service was closed on both the servers I was attempting connection by the hosts.

This works fine for me. I copied and pasted your code and ran it locally without any problems. If you encountered a connection timeout error it would seem to be a temporary error. The only error I noticed was when you are printing the hotel description the property name was incorrect:
echo urldecode($xml->HotelDescription);

Related

Curl error: operation aborted by callback

So I have obviously googled the error - but PHP (PHP 7.4.4 (cli)) curl gives me the error:
Curl error: operation aborted by callback with the following code:
private function curl_post($url,$post,$file = null,$file_type = 'audio/wav'){
$ch = curl_init($url);
if (!empty($file)){
$post['SoundFile'] = new CURLFile(UPLOAD_PATH.$file,$file_type,$file);
}
// Assign POST data
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if(curl_errno($ch)) echo 'Curl error: '.curl_error($ch);
curl_close($ch);
print'<pre>Curl (rec): '."\n";print_r($result);print'</pre>';
}
I control both (Ubuntu) servers and have rebooted them both. I am posting a fairly large amount of data but in the Google searching this didn't seem to be what is triggering the curl_error. Does anyone know what is causing it? It was working perfectly fine and then it stopped.
Additionally putting file_put_contents(time().'.txt','log'); as a break in my receiving server does log the response. So its clearly landing in the right area.
Additionally what I will say is that the 2 servers talk a number of times to each other through curl (so one curls to one then back a bit). Furthermore - error 42 is the CURL response but https://curl.haxx.se/libcurl/c/libcurl-errors.html doesn't seem to provide much help. I've tried tracing the various calls to each other and can't see why it is breaking - it errors/breaks before the post/calls even occur.
So I found the answer and I hope this helps anyone else in this situation. The reason being was because the file was missing on the server for the CURLFile (having previously been there). My code now reads:
if (!empty($file) && is_file(UPLOAD_PATH.$file)){
$post['SoundFile'] = new CURLFile(UPLOAD_PATH.$file,$file_type,$file);
}
And this no longer generates the error. The key was that it errored even before submitting the post but the error itself wasn't that helpful until I broke it down in a separate test script and added the file element back in.

file_get_contetnts and soap client don't work with long query

Sorry if this is duplicate,I would think it would be but couldn't find anything
I have an application using soap client web-service to connect to other server but recently I found out the requests with long text do not send to web-service.
I thought it related to some PHP configs and set post_max_size to 200M and set max_input_vars to 10000 but the problem did not solve ...
today I found out file_get_contents don't work as well as soap client in my server and I am sure one of php configuration made this problem.
here is the code that dont work and when I run this just wait for a while and display nothing ( white page)
$url="http://somedomain.com/webservice.php?Username=user&Password=pass&Det=13218%5E04134414401%5E7015373%5E3%5E51%5E14%252F2141-9305-310209-424926%25D8%25A2%25D9%2585%25D9%2588%25D8%25B2%25D8%25B4%2B%25D9%2583%25D8%25A7%25D8%25B1%25D8%25A8%25D8%25B1%25D8%25AF%25D9%258A%2BHTML%2B5.0%2B%2526%2BCSS%2B3.0%2B%25D9%2587%25D9%2585%25D8%25B1%25D8%25A7%25D9%2587%2B%25D8%25A8%25D8%25A7%2BResponsive%2BWeb%2BDesign%25D9%2585%25D8%25B1%25D8%25AC%25D8%25B9%2B%25D8%25A2%25D9%2585%25D9%2588%25D8%25B2%25D8%25B4%25D9%258A%2B%25D8%25A8%25D8%25B1%25D9%2586%25D8%25A7%25D9%2585%25D9%2587%25E2%2580%258C%25D9%2586%25D9%2588%25D9%258A%25D8%25B3%25D9%258A%2BASP.NET%2B4.5%2B%25D8%25AF%25D8%25B1%2BC%2523%2B%2528%25D8%25A7%25D8%25B2%2B%25D9%2585%25D8%25A8%25D8%25AA%25D8%25AF%25D9%258A%2B%25D8%25AA%25D8%25A7%2B%25D9%25BE%25D9%258A%25D8%25B4%25D8%25B1%25D9%2581%25D8%25AA%25D9%2587%2529%25D9%2585%25D8%25B1%25D8%25AC%25D8%25B9%2B%25D9%2583%25D8%25A7%25D9%2585%25D9%2584%2BASP.Net%2BMVC%2B4%5E2825%5E819700%5E1%5E0%5E%25D8%25A7%25D9%2585%25DB%258C%25D8%25B1%25D8%25B1%25D8%25B6%25D8%25A7%2B%25D8%25A7%25D8%25B3%25D8%25AF%25D9%25BE%25D9%2588%25D8%25B1%5E%25D8%25AA%25D8%25A8%25D8%25B1%25DB%258C%25D8%25B2%2B-%2B%25DA%2586%25D9%2587%25D8%25A7%25D8%25B1%25D8%25B1%25D8%25A7%25D9%2587%2B%25D9%2584%25D8%25A7%25D9%2584%25D9%2587%2B%25D9%2585%25D8%25AC%25D8%25AA%25D9%2585%25D8%25B9%2B%25D8%25AA%25D8%25AC%25D8%25A7%25D8%25B1%25DB%258C%2B%25D9%2584%25D8%25A7%25D9%2584%25D9%2587%2B%25D8%25B7%25D8%25A8%25D9%2582%25D9%2587%2B%25D8%25A7%25D9%2588%25D9%2584%2B%25D9%2588%25D8%25A7%25D8%25AD%25D8%25AF%2B5%5E5178735466%5Einfo%2540pendarepars.com%5E154000%5E12320&Ordertip=1";
echo file_get_contents($url);
You can avoid this issue by sending the request as a post not Get
You should check what the server is responding with using cUrl. This snippet should help you on your way.
// Prep cUrl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
// Output info
echo "<pre>";
if(curl_error($ch))
{
echo "Error: ".curl_error($ch)."\n";
}
else
{
print_r(curl_getinfo($ch));
echo htmlentities($return);
}
Look for http_code, if it's 414 then the server can't handle the length of the URI.

simplexml_load_file not working on live website ONLY

I have recently put my website online and found simplexml_load_file problem.
It works perfectly on local server (MAMP) but not live on the website.
Code is as below:
<?
$source = simplexml_load_file('http://domainname.com/file.xml') or die("Something is wrong");
echo $source->Result;
?>
The above code displays following message on live website: "
Something is wrong
And same code displays following message on local server MAMP:
Success
And file.xml is:
<?xml version="1.0"?>
<!DOCTYPE ValidateUser>
<ValidateUser>
<Customer>john</Customer>
<Result>Success</Result>
</ValidateUser>
Thanks in advance.
It sounds like your hosting provider has disabled PHP's ability to open URL's as files.
Run the following snippit of code on your server. If the result is "0", then this feature
has been disabled.
<?php var_dump(ini_get('allow_url_fopen')); ?>
If its disabled you might need to use something like CURL to fetch the XML file before
processing it. You could then use simplexml_load_string() instead of simplexml_load_file()
Here is an example of how to use CURL:
http://davidwalsh.name/curl-download
If CURL is also not available you will have to discuss alternatives with your hosting
provider.
So, using the code from the link above, you could do something like this:
/* gets the data from a URL */
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_content = get_data('http://davidwalsh.name');
$source = simplexml_load_string($returned_content) or die("Something is wrong");
echo $source->Result;

This curl command doesn't work for some reason

I have a URL that I need to "call" from a PHP script. The URL makes a phone call using Tropo's API, so it's easy to verify if it was called.
My PHP looks like this:
<?php
$oid=$_GET["oid"];
$notify_url = "http://mydomain.com/somepath/".$oid;
echo $notify_url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $notify_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
mail('me#gmail.com', 'cURL', "Did a cURL");
?>
This prints out the $notify_url variable, and when I take the printed value and enter it into a browser window, I get the desired result i.e. the phone call to my phone.
I have tried this on two web hosts that claim they support CURL, one is paid (crazydomains.com.ayu - just got off the phone to support) and the other is 000webhost.com
Am I doing something wrong? This one is kind of confusing, since it should be so simple.
EDIT: I receive the mail as expected.
EDIT 2: If you have any ideas about how I can debug this, I would appreciate it.
EDIT 3: As Juhana suggested I added echo curl_error(); after curl_exec and I got this error ...
Warning: Wrong parameter count for curl_error() in /home/a5352876/public_html/curl.php on line 15
EDIT 4: changed the echo curl_error() to echo curl_error($ch) and got the message couldn't connect to host so that seems to be the problem.
Now the question is, why can't it connect to a host that is easily accessible through a browser, is there anywhere I can look for that?
If your host has HTTP wrappers enabled, and the allow_url_fopen config option is enabled, then you don't need cURL, especially for such a simple request.
It looks like all you need to do is open a URL, which you can easily do with fopen():
$oid = $_GET["oid"];
$notify_url = "http://mydomain.com/somepath/".$oid;
$fh = fopen( $notify_url, 'r');
fclose( $fh);

CURL can't connect? Just hangs and doesn't respond. PHP has it enabled

I have a simple PHP function on a friend server which I've checked and has PHP CURL enabled.
The function is:
function sw_fetch_code($apikey='',$email=''){
$url = "http://www.domain.com/xxx/api.php?getcode=1&apikey=".$apikey."&email=".$email."";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
if(!empty($obj)){
if($obj->status == 200){
return $obj->code;
}else{
return $obj->status;
}
}
}
As you can see this is very simple and I've tested it and works on localhost and internal to my own server. The url returns as expected. However it just doesn't give any response when this function is called on my friends server.
Any ideas what could cause this?
First :
check from the "friend" server if the URL works, as you donot have POST params, you can check with the exact query and get expected results. See if you can get the results on the browser on the friend server. If you don't have a GUI try wget on the command line. See if you get results. If you do go to next step, if you don't cURL isn't the problem. "friend server" isn't able to see your domain. Could be network issue / hosts etc .. (more on that if its the case)
Second:
If you see results on step 1. Try this and see if you get anything:
$handle = fopen($url, "rb");
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle,1024);
}
If you get response to this, then there is something wrong with cURL.
Does the curl_exec() call fail immediately, or does it hang for 30 seconds or so until it times out? If the latter, you may want to check for a firewall issue.
What does curl_getinfo($ch) tell you?
I think you should begin with standart checks:
If php is compiled with php_curl extension (or the extension is available as shared object). You can check it by putting a
<?php
if (!extension_loaded('curl'))
{
if (!dl('curl.so')) {
die('Cannot load php_curl extension');
}
}
?>
If the extension is loaded there might be a problem with dns/firewall on the friend's server. There also might be a requirement to use proxy server.

Categories