apache - Curl in PHP returns null on AWS EC2 instance -
i have aws ec2 , apache server setup. website pages working except 2 places whereever sued curl. following pointers per issue search online, working expected:
curl showing in phpinfo(). curl command accessible , running accross. apache2 have curl installed. curl command cli working fine. (curl http://localhost , curl www.google.com) both working.
when ran following function, url when run in browser $url, returns data expected. when run php, none of echo commands ever returned values. further, values seem null.
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 first function, based on of apis dependent on. above php file in var/www/html/folder1. when access main api trying access in browser, $hostname.'/folder1/app1.php?all', return data hosted on local apache server , remote domain host server well. not working aws ec2.
i answered similar curl problem yesterday replicate/debug. code bit generic , not knowing remote server address can suggest debug curl request.
see get request works python 'requests' library, not work curl
you need check if curl returns errors , debug 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 errors : <br/>' . $flight_model_data . " ended\n\n <br/><br/>"; echo "<br/>curl info : " . json_encode(curl_getinfo ($handle) ); }
Comments
Post a Comment