phpcurlamazon-ec2bluehost

PHP Curl Not connecting to a url with IP Address and PORT


Have made and deployed an app on an AWS EC2 instance, and accessing the app via ip_address:port, where ip_address is the elastic ip associated with the instance.

I am able to make API calls to this app using postman, and via cmd.

However, when I try to make POST request using PHP Curl on Wordpress (hosted on Bluehost), I keep getting the

Failed to connect to ip_address port XXXX after 0 ms: Connection refused.

Any ideas of what I might be missing?

'''php

$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http:ip_address:8000/payments/accounts/v1',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
CURLOPT_TIMEOUT => 1000,
CURLOPT_CONNECTTIMEOUT => 1000,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_CUSTOMREQUEST => 'POST',));
print_r(curl_error($curl));
curl_exec($curl);
curl_close($curl);

'''

Getting this response

'''code Failed to connect to id_address port 8000 after 0 ms: Connection refused '''


Solution

  • Using the redacted ip and a slightly modified version of your code as follows:

    $url='http://X.XXX.XX.XX:8000/payments/accounts/v1';
    $curl = curl_init();
    curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 1000,
            CURLOPT_CONNECTTIMEOUT => 1000,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_POST=>true
        )
    );
    $res=curl_exec( $curl );
    curl_close( $curl );
    
    printf('<pre>%s</pre>',print_r( $res, true ));
    

    Yields:

    {"detail":"Authentication credentials were not provided."}
    

    Perhaps if you were to actually include and submit some post data in the CURLOPT_POSTFIELDS option you might find a different response.