Curl in PHP returns null to AWS EC2 instance

I have AWS EC2 setting up an Apache server. All pages of the website work, with the exception of two places where I sued CURL. The following pointers, in accordance with this problem, look for online that work as expected:

CURL is displayed in phpinfo (). The CURL team is available and works in different ways. Apache2 has CURL installed. the curl command from the CLI is working fine. (curl http: // localhost and curl www.google.com) both work.

When I ran the following function, url when launched in the browser, $ url returns the data as expected. But when I run from PHP, none of the echo commands ever returned a value. In addition, the values ​​seem to be zero.

function get_patients () {$ hostname = file_get_contents (' http://169.254.169.254/latest/meta-data/public-hostname '); $ url = $ hostname. '/folder1/app1.php? all ';

//echo $url; ///$url .= urlencode($query);//.'&key='.$key; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); //echo $curl; curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT , 400); curl_setopt($curl, CURLOPT_TIMEOUT, 400); $data = curl_exec($curl); curl_close($curl); //decoding request $result = json_decode($data, true); //print_r( $data); return $result; } 

This is the first feature that most APIs are based on. This above php file is in the var / www / html / folder1 file. when I access the main API I'm trying to access in a browser, $ hostname. '/folder1/app1.php? all ' , returns data as hosted on the local Apache server and even the remote domain. Its just not working with AWS EC2.

-1
source share
2 answers

Yesterday I answered a similar twist problem where I could replicate / debug. Your code is a little generalized and does not know the address of the remote server, I can offer you to debug your curling request.

See GET Request works with python request library but does not work with curl

you need to check if curl returns any errors and debugs as below

 if(curl_exec($handle) === false) { echo 'Curl error: ' . curl_error($handle); } else { echo 'Char encoding ' . mb_detect_encoding($flight_model_data) . ' <br/><br/>'; echo 'Operation Completed without any errors : <br/>' . $flight_model_data . " ENDED\n\n <br/><br/>"; echo "<br/>Curl info : " . json_encode(curl_getinfo ($handle) ); } 
0
source

Thanks for the answer. With continuous troubleshooting, I received this fix. You need to change the configuration that needs to be changed in php.ini in the Apache server configuration, just make sure that the settings and versions are the same in both PHP5 and the Apache2 folders on the server. Also, PHP5 MUST have PHP5-libcurl installed, which was already on my server. Thanks everyone for the quick reply.

0
source

All Articles