Is there anyway to make simpleXML work on web host - php

<?php
$twitter_url = 'http://twitter.com/statuses/user_timeline/ishrikrishna.xml?count=1';
$buffer = file_get_contents($twitter_url);
$xml = new SimpleXMLElement($buffer);
$status = $xml -> status;
$tweet = $status -> text;
echo $tweet;
?>
I used this code to fetch tweets and it works successfully on localhost but not on my webhost, I tried this script on two webhosting services.
The problem i've noticed is that functions like file_get_contents(), simplexml_load_file() failed to fetch data from xml file(ex. rss files) stored on another server.

I believe this means you have the fopen URL wrappers turned off. See http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen You will probably not be able to turn these on if you are using a shared web server.
You may be able to use cURL to fetch remote pages instead:
$ch = curl_init('http://twitter.com/statuses/user_timeline/ishrikrishna.xml?count=1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$buffer = curl_exec($ch);
This will only work you have the cURL extension installed and enabled on your webhost.
See the PHP cURL documentation: http://www.php.net/manual/en/book.curl.php
Edit: corrected curl_setopt call

SimpleXML is new in PHP 5. I think almost all webhosts have PHP 5 installed, but in case your host is still using PHP 4 that may be the reason your script isn't working.

Related

file_get_contents/curl PHP Function Not Working For IP Address

My server contain a file at http://XX.XX.XX.XX:7550/check.txt and in which only Online is written. Now I am on my shared hosting and I want to get Online in my shared hosting php file for that I am using the below code in my shared hosting php file.
<?php
var_export(ini_get('allow_url_fopen'));
$uri = 'http://XX.XX.XX.XX:7550/check.txt';
$result = file_get_contents($uri);
echo $result;
?>
But I am only getting '1' instead of '1' Online. So can you answer my why not working when I enter my server IP?
Note: And when I am adding http://www.google.com instead of http://XX.XX.XX.XX:7550/check.txt then I am getting whole Google page.
Important Note: I am able to open http://XX.XX.XX.XX:7550/check.txt in my web-browser directly via proxy means no firewall blocking is in my server (Ubuntu Server 14.04 LTS).
UPDATE:
I also tried CURL and that's also not working with http://XX.XX.XX.XX:7550/check.txt but working with http://www.google.com.
<?php
function curl_load($url){
curl_setopt($ch=curl_init(), CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$url = "http://XX.XX.XX.XX:7550/check.txt";
echo curl_load($url);
?>
Try to use cUrl instead of file_get_contents.

Curl or Simple XML? - Apache or PHP settings?

I have a problem with my server setting I guess. I have a code which fetches code from another server. I don't know why, but the code is not working with my VPS, but it is working with a simple shared hosting account...
Here is the code:
$post = array(
'KEY' => 'somekey',
'format' => 'xml'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'xxxxxxxxxx.com' . http_build_query($post));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
$xml_obj = simplexml_load_string($response);
$json = json_encode($xml_obj);
$array = json_decode($json, true);
curl_close($ch);
It is working like a charm if I run it on my shared hosting server, but it just have some problem (which it does NOT output) and the script does not run. I have tested that I got 1 (true) for this statement: $json = json_encode($xml_obj); But not for any further code. So there might be somewhere the the problem.
I have also checked if xml DOM is enabled, and it is. I have also checked CURL and json, and both of them are enabled.
Can someone help me? I can't do anything without error messages, and I cannot figure out what could be the problem. :/
It seems like you may have remote allow_url_fopen not enabled.
Put this at the beginning of your script:
ini_set("allow_url_fopen", 1);
To enable curl_exec, you need to modify your php.ini and remove it from the last of disallowed functions. To find out your php.ini, you can call this at the beginning of your script once:
phpinfo(); die();
That will tell you which php.ini file to use (look for the php.ini string). Once you modify php.ini, you will need to restart your web server.

curl_exec($ch) not executing on external domains anymore, why?

I was using cURL to scrape content from a site and just recently my page stated hanging when it reached curl_exec($ch). After some tests I noticed that it could load any other page from my own domain but when attempting to load from anything external I'll get a connect() timeout! error.
Here's a simplified version of what I was using:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://www.google.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
$contents = curl_exec ($ch);
curl_close ($ch);
echo $contents;
?>
Here's some info I have about my host from my phpinfo():
PHP Version 5.3.1
cURL support enabled
cURL Information 7.19.7
Host i686-pc-linux-gnu
I don't have access to SSH or modifying the php.ini file (however I can read it). But is there a way to tell if something was recently set to block cURL access to external domains? Or is there something else I might have missed?
Thanks,
Dave
I'm not aware about any setting like that, it would not make much sense.
As you said you are on a remote webserver without console access I guess that your activity has been detected by the host or more likely it caused issues and so they firewalled you.
A silent iptables DROP would cause this.
When scraping google you need to use proxies for more than a few hand full of requests and you should never abuse your webservers primary IP if it's not your own. That's likely a breach of their TOS and could even result in legal action if they get banned from Google (which can happen).
Take a look at Google rank checker that's a PHP script that does exactly what you want using CURL and proper IP management.
I can't think of anything that's causing a timeout than a firewall on your side.
I'm not sure why you're getting a connect() timeout! error, but the following line:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
If it's not set to 1, it will not return any of the page's content back into your $contents.

function/command to retrieve web content (besides from wget or file_get_contents)

I need to retrieve a web content. I usually use wget but it's giving me an "Internal Server Error" this time. I also tried using file_get_contents(), it works when I'm using MAMP installed in my Mac, but when I run it in our server, it's not doing anything and prints no error message. Is there any other way to do this? Below are the code that I used.
<?php
echo "Retrieving Traffic Updates";
$source = file_get_contents('http://www4.honolulu.gov/hpdtraffic/MainPrograms/frmMain.asp?sSearch=All+Incidents&sSort=I_tTimeCreate');
echo $source;
?>
Thanks in advance!!
I don't know how to reply to all of you so I just added it here. CURL WORKED PERFECTLY. Thanks a lot guys.
I just have to ask though why file_get_contents() won't work even if it's enabled in the php.ini?
You can use cURL:
<?php
echo "Retrieving Traffic Updates";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www4.honolulu.gov/hpdtraffic/MainPrograms/frmMain.asp?sSearch=All+Incidents&sSort=I_tTimeCreate");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$source = curl_exec($ch);
curl_close($ch);
echo $source
?>
I would guess that you don't have the fopen wrappers enabled. To test, do:
var_dump(ini_get('allow_url_fopen'));
If this returns false (or 0, can't remember which), you can't use file_get_contents to open a remote file.
If cURL is installed, you can use that to access remote files.
allow_url_fopen may be turned off by your web host. You should see if this can be turned on. If not, cURL may still available so you should try using that instead.
If cURL is available to you, use that. You can determine if cURL is installed by doing a phpinfo();. For more information on cURL: PHP: cURL
Please reference http://davidwalsh.name/download-urls-content-php-curl for a solid example of using PHP's implementation of CURL to fetch the contents located at a given URL.
I believe you'll find the process fairly straightforward.
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
return curl_exec($ch);
curl_close ($ch);
}
$html = curl("http://www4.honolulu.gov/hpdtraffic/MainPrograms/frmMain.asp?sSearch=All+Incidents&sSort=I_tTimeCreate");

Send files from LAMP stack to Windows SFTP server via PHP

I'm having a good deal of trouble sending a file from a linux server to a windows server over SFTP via PHP.
I seem to be connecting just fine, but it always throws an error that I can't create the file on the remote server's end. It's possible that I am messing up at the syntax for the file location.
I have tried two ways now, one using ssh2_scp_send(), and another trying
fopen(ssh2.sftp://D:/path/file.csv)
Also, logging into the sftp server via a client puts me at my home folder (ie D:\path\to\home) but if I do a
ssh2_exec($connection, 'cd');
and print the stream to the screen, it shows me that my ssh session is currently in the windows filesystem on the C drive.
I was hoping someone would have some advice on this. And I'm not married to this method. I'm using php on my end because it's all coming from a drupal module, but I could always try and incorporate another method.
If all you want to do is send one file, curl works, I suppose, but if you want to do anything more - like maybe verifying that the file has been uploaded, upload multiple files, use publickey authentication, or whatever, curl just isn't versatile enough for that.
My recommendation would be to use phpseclib, a pure PHP SFTP implementation.
CURL can use th sftp library.
$localfile = 'test.txt';
$ch = curl_init();
$fp = fopen ($localfile, "r") or die('Cannot open textfile');
curl_setopt($ch, CURLOPT_URL,"sftp://123.123.123.123:10022/username/test.txt");
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
if(curl_exec($ch)){
echo 'File was successfully transferred using SFTP';
}else{
echo 'File was unable to be transferred using SFTP';
}
curl_close ($ch);

Categories