PHP5 cannot receive data from other websites - php

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

Related

API not work on Third Level Domain

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

moved from cpanel to plesk and now php and curl not working that worked before

This code was working on Cpanel, we moved to a dedicated server that has Plesk (I hate plesk, terrible, but it does work, but Curl not working...)...
This is the code that we have that worked flawlessly on Cpanel server:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$_pgCheck");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$pgCode = curl_exec($ch);
curl_close($ch);
$_pgCheck url has this:
https://example.org/system/page/?var=longkey&user=username
something like that.
I had it output the domain it runs, and went there myself manually, and that page does output a response.
So the curl is not executing.
I added this line to test it:
if($_SERVER['REMOTE_ADDR'] == "x.xx.xxx.xx") { // My IP address so only I see debuggin...
if(!curl_init($ch)){
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}
}
and it does die, but with this:
Error: "" - Code:
so no response in curl_error or curl_errno
How do I test it to figure out why it is not working?
Or do you see the error in the curl_init or curl_setopt I need to change?
UPDATE: I went into Plesk - Domains - domain name - PHP Settings and at the bottom, where I can add additional directives, I added:
extension=php_curl.dll
to make sure php compiled Curl.
but that did not change anything after I saved it.
Pretty sure after I saved it, Plesk restarted apache.
Thanks
-Rich
First thing first. Do a phpinfo() on the new server and try to see if curl is enabled. You can just try to find curl in the output of phpinfo() (ctrl+f). If you cannot see curl than you probably need to install curl on that server's php. For that try googling - how to install curl module in php.

Google ReCaptcha - Getting Challenge Response on shared server

I have a site that uses the new version of Google ReCaptcha (I am not a robot version), and am having trouble getting the challenge response on my shared server.
I cant use curl or file_get_contents due to restrictions on the server. Is there any other ways to get the response?
The code I was using locally, that does not work on the live site is:
CURL
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;
}
$response=$this->get_curl_response("https://www.google.com/recaptcha/api/siteverify?secret=SECRET&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
File Get Contents
$response=$this->get_curl_response("https://www.google.com/recaptcha/api/siteverify?secret=SECRET&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
This turned out to be a problem with the hosting company that could not be resolved, the challenge response from the captcha was being blocked by the hosting company's firewall, and therefore the capthcha always failed.
Because I am on a shared server they could not enable file_get_contents() as it would be a security risk for all the sites on that server.
I installed PHP Captcha (https://www.phpcaptcha.org/) as an alternative, whoch works fine as all the logic is done locally.

How to fetch the url content using php curl or file_get_content on the same server?

I have some code to convert a PHP page to HTML:
$dynamic = "http://website.net/home.php";
$out = "home.html" ;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"$dynamic");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file = curl_exec($ch);
file_put_contents($out, $file);
This works perfect in localhost but it takes too much time/doesn't work on the live site.
I've tried php_get_contents, but that also doesn't work.
Note:
http://website.net/home.php page is in the same site where the code is hosted.
curl is enabled and allow_url_fopen is on as per phpinfo() in both localhost and at server.
EDIT:
It works fine when using other website's page instead of my website.
The site's target page perfectly loads in my browser.
The web page of my website is loading fast as usual but when I use curl or file_get_contents, it's too slow and even can't get output.
I think you have DNS resolving problem.
There is two ways you can use your website's locahost instead of the external domain name.
1. If you have your own server / VPS / Dedicated server,
Add entry in vim /etc/hosts for 127.0.0.1 website.net or try to fetch the content with localhost(127.0.0.1).
2. If you are using shared hosting, then try to use below url(s) in your code,
http://localhost.mywebsite.net/~username/home.php.
OR
Try to call http://localhost.mywebsite.net/home.php
Try this to fetch the url content -
$dynamic = TRY_ABOVE_URL(S)
$out = "home.html" ;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$dynamic);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file = curl_exec($ch);
if($file === false) {
echo 'Curl error: ' . curl_error($ch);
}
file_put_contents($out, $file);
To investigate the reason why is it not working on live server, Try this:
$dynamic = "http://website.net/home.php";
$out = "home.html" ;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $dynamic);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file = curl_exec($ch);
if($file === false)
{
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
file_put_contents($out, $file);
I think it depends on the provider SSL configuration.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
Check for know smth. about SSL configuration of your provider.
Looks like it is a network routing issue, basically the server can't find a route to website.net(itself). This has is a server issue and not a PHP issue. The quickest solution is to edit the hosts file on the server and set website.net to 127.0.0.1.
On linux servers you will need to add the following line to the bottom of /etc/hosts to:
127.0.0.1 website.net
Alternatively you can try fetching http://127.0.0.1/home.php but that will not work if you have multiple virtual hosts on the server.
Try this one:
file_put_contents("home.html", fopen("http://website.net/home.php", 'r'));
but curl should work as well. If you have SSH access to the server, try to resolve your domain name using ping or something. It is possible to have a local DNS issue - it will explain why you can download from external domains.
For my example, you'll need allow_fopen_url to be On, so please verify that by phpinfo() first.

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

Categories