API not work on Third Level Domain - php

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

Related

Can't load file from URL, but works with local path

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);

PHP - Retrieve Facebook image in Google Compute Engine

I've tried to retrieve the image data of my Facebook profile picture, using both file_get_contents and curl.
The problem occurs on my Google compute engine instance, while on any other server (localhost - mamp, AWS) the script works fine.
An example for one of the scripts I was using
<?php
var_dump(
json_decode(
file_get_contents("https://graph.facebook.com/_FACEBOOK_ID_/picture?width=720&height=720")
)
);
Please keep in mind that I've tried using the parameter redirect=false, and accessing the image url I've got in my json response returned false as-well.
Also, I've tried using wget in SSH to the image's url, which returned (403) Forbidden.
My assumption is that I need to configure something differently in my server, not PHP, but because I'm able to retrieve any other image, with the same script - I'm not sure what.
I've already experienced this exact problem,
Ignoring SSL verification while using cURL did the trick for me.
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Ignore SSL verification
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/_FACEBOOK_ID_/picture?width=720&height=720");
$data = curl_exec($ch);
curl_close($ch);
var_dump($data);

PHP curl works on localhost but not on the web server?

The below php code is making a REST API call that returns json object. This works fine when i use it on my localhost using WAMP Absolutely no problem.
However when i push this app on the server it would TimeOut and display 503 Service Unavailable.
I checked the logs it has an entry :
The TimeOut Specified has expired.
I contacted my admin he just said this app listens to PORT=64665 and HOST=0.0.0.0. What does that mean? What more changes do i need to make in my code to make it work on the server ? Help
<?php
$url = "http://xyz.net/v2/plan/"; // I have changed the REST URI API Link for security reasons
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROXY, false);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
// Will dump json
var_dump(json_decode($result, true));
$response = "http://xyz.net/v2/plan/";
echo $response;
?>
Check your server's /etc/hosts file, there could be the problem, ie. domain mapped to another (wrong) IP address.

Server ignores cUrl code lines php

iam using that php code in the localhost and working fine
$ch = curl_init($src);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
when i move it to the remote server the script ignores that code
and doesn't give me a result (still giving me the original url )
note: i use cURL here to give me the true url after redirect
Maybe there is some difference in the configuration of the remote server.
phpinfo();
Shows the configuration of php and of the curl module

PHP5 cannot receive data from other websites

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

Categories