简体   繁体   中英

issues with accessing URL with cURL

I have this web service which returns a JSON:

 http://54.246.99.134:8080/services-app/services/Users/GetConfigForMetroAndLang?metroAreaId=1&langId=39

When I access it using my browser, I get my JSON and all is working OK. When I try to grab it with CURL or file_get_contents the page just keeps loading until : failed to open stream: Connection timed out

Other URLS are working great when I try to get them from the server (any other address).

** edit ** Thanks for the comments

My code is very straight forward :)

$curl = curl_init('http://54.246.99.134:8080/services-app/services/Users/GetConfigForMetroAndLang?metroAreaId=1&langId=39'); 
curl_setopt($curl, CURLOPT_FAILONERROR, true); 
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101       Firefox/12.0');
 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);   
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 $result = curl_exec($curl); 
 print_r($result) ; 

I now understand it works from a local host of one of my team mates... so it is probably a server / firewall issue? any direction maybe? ** /edit **

Would really appreciate any idea. Thanks!

I checked with your code, it is working fine for me.

Most probably the issues will be that your server cannot connect to an external resource, may be due to firewall restrictions.

Also allow_url_fopen needs to be On in your PHP settings for file_get_contents to work with URLs. You can check with this by printing out phpinfo() in any of your page.

Also don't use file_get_contents to get the contents or data because it does not work well at all with getting remote files. It also does not deal with slow network connections or redirects, and does not return error codes. You should use curl , it is also faster than file_get_contents

Regarding your issue, I think it will be a firewall issue. Please check with your firewall settings.

Also, there is a mistake in your code. You are mixing $ch and $curl. It should be:

$curl = curl_init('http://54.246.99.134:8080/services-app/services/Users/GetConfigForMetroAndLang?metroAreaId=1&langId=39'); 
curl_setopt($curl, CURLOPT_FAILONERROR, true); 
curl_setopt($curl , CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101       Firefox/12.0');
 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);   
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 $result = curl_exec($curl); 
 print_r($result) ; 

You can use curl_getinfo to get information regarding a specific transfer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM