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.
Related
I am trying to load an XML file, but for some reason I can't use a URL.
allow_url_fopen is set to On.
I used this, which didn't work:
$xml = simplexml_load_file("https://example.corn/test.xml");
But this does work:
$xml = simplexml_load_file("../test.xml");
I also tried file_get_contents:
$xml = file_get_contents("https://example.corn/test.xml");
and even cURL:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.corn/test.xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xml = curl_exec($ch);
curl_close($ch);
For each example I am displaying the output with var_dump($xml);, which always shows bool(false) when using a URL and shows the correct XML data when using a local path.
I would just use the local path, but this is just for a test and the actual XML file I am going to fetch is from a different site.
I tested it on a local server and it worked, but on my hosted server, it doesn't work at all. They are using the same php.ini. My hosted server is using php-fpm, I'm unsure if that could be a factor, or if there is something that has to be changed with it.
This might be a configuration error that I am unaware of, because the code should work, and the link definitley exists, because I can click on it and see the correct xml in the browser.
What could be causing this issue?
Turns out that I had my openssl.cafile property set to some location that didn't exist.
For some reason no errors were showing when using the commands above.
I think the specified url is not valid or doesn't exist
Read about curl in the PHP docs
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
i have some problems.
I need to keep data from coinmarketcap. When I was developing on localhost it worked well.
But on third level domain coinfollow.altervista.org i can not receive the data
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.coinmarketcap.com/v1/ticker/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
curl_close($ch);
$outputdecoded = json_decode($output, true);
echo $outputdecoded;
I try in another domain mywebsite.com and it worked. I think that the problem is coinfollow.altervista.org domain.
I need to save coinmarketcap data into my database with a simple query.
Does anyone know a solution?
It sounds like there is a server config issue on coinfollow.altervista.org, such as curl not being enabled for php.
You can run phpinfo(); to see if curl is installed.
If it is installed try running echo curl_error($ch) to see if there are any errors returning from curl
I have my virtually hosted web server. I installed it using VirtualBox, and it uses the Ubuntu Server system. Recently, I was in a need to get data from Google Maps Geocode service. Firstly, I tried using the next code:
file_get_contents(URL);
After getting the timeout error, I tried using cURL also:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://maps.google.com/maps/api/geocode/json?address=" . $gm_address . "&sensor=false");
$result = curl_exec($ch);
curl_close($ch);
Yet again, I got a timeout error.
I suspect that Ubuntu does not allow PHP to make calls to other websites. I am not an Linux or Ubuntu expert, so I did not know how to tackle the firewall settings, or settings that would allow PHP to make those calls.
In short, how do I change the settings that would allow PHP to get data from other websites?
Try this cURL code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://maps.google.com/maps/api/geocode/json?address=" . $gm_address . "&sensor=false");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
Does the text now appear in $result?
You may want to check your firewall settings it may be blocking the other sites.
Maybe your php.ini has curl disabled
look for disable_functions in /etc/php.ini
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");
<?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.