1

I have AWS EC2 , Apache server setup. All website pages are working except for two places whereever I sued CURL. Following pointers as per this issue search online, which are working as expected:

CURL is showing up in phpinfo(). CURL command is accessible and running accross. Apache2 does have CURL installed. curl command from CLI is working fine. (curl http://localhost and curl www.google.com) both are working.

When I ran following function, url when run in browser $url, returns data as expected. But when I run from PHP, none of the echo commands ever returned a values. Further, values seem to be 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 is the first function, based on which most of APIs are dependent on. This above php file is in var/www/html/folder1. when I access the main API that I am trying to access in browser, $hostname.'/folder1/app1.php?all', does return data as hosted on a local apache server and even remote domain host server as well. Its just not working with AWS EC2.

5
  • Also, Please note that No firewalls are set and the status shows inactive when I ran firewall commands. Further more, Inbound and Outbound rules do allow 80/443/SSH ports. SInce rest of PHP requests were working, I suspect this is only to do with CURL. Thanks again for any help. Commented Aug 24, 2015 at 23:18
  • This smells like SELinux... try running /usr/sbin/setsebool httpd_can_network_connect 1 (this will temporarily allow apache to access to the network, which is what curl would be doing)... if it works, /usr/sbin/setsebool -P httpd_can_network_connect 1 to make it permanent) Commented Aug 24, 2015 at 23:21
  • I ran this command. No such file or directory error. :/ Commented Aug 25, 2015 at 1:33
  • Hmm... you could try setsebool without the full path (in case your linux puts it somewhere other than /usr/sbin) (also note that you need to run this as root) (See e.g. stackoverflow.com/questions/22640846/… for a similar issue) Commented Aug 25, 2015 at 12:42
  • I did but its not SELinux. its Ubuntu. I got this fixed and just posted the fix for my issue. Just kept adding work around after work around and it fixed. Strange though. Commented Aug 27, 2015 at 19:00

3 Answers 3

2

Thanks ton for reply. Upon continuous troubleshooting I got this fixed. Needed a change in configuration to be modified in php.ini in apache server configuration, just make sure settings and versions are same in both PHP5 as well as Apache2 folders on server. Also PHP5 MUST have PHP5-libcurl installed, which was already there on my server. Thank you everyone for swift response.

Sign up to request clarification or add additional context in comments.

Comments

1

I answered a similar curl problem yesterday where i could replicate/debug. Your code is a bit generic and not knowing the remote server address i can suggest you debug your curl request.

See GET Request works with python 'requests' library, but does not work with curl

you need to check if curl returns any errors and debug just like the 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) );
}

3 Comments

Thank you for swift reply. But its not displaying anything related curl. I think curl_init() is never successful. Here is the code:
Displays none of the if or else block statements.
Ok lets check if curl is actually enabled? in a PHP file 'function_exists('curl_version');' or in a command line 'php -i | grep curl'
0
sudo apt-get install -y php8.1-cli php8.1-common php8.1-mysql php8.1-zip php8.1-gd php8.1-mbstring php8.1-curl php8.1-xml php8.1-bcmath

I have same problem, but solved after installation of php8.1-curl.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.