Question

I try to send data to my application with a POST. The url is a bit tricky, it' s https only and on a specific port

Here is my code

$url = 'https://mysite.com/foo.php';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);

if ($type == "POST") {
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}

if ($environment == 'test') {
   curl_setopt($curl, CURLOPT_PORT, 1234);
}


$result = curl_exec($curl);

if (curl_errno($curl) > 0) {
   $result = 'Internal server error, error: ' . curl_errno($curl);
}
curl_close($curl);

It returns a error 7 Failed to connect() to host.

What did i made wrong?

UPDATE :

After bodi0 advices i add

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl,CURLOPT_FOLLOWLOCATION, true);

UPDATE2 :

It was a network issue, my code was ok...

Was it helpful?

Solution

Try to setting the port number like this:

curl_setopt($curl, CURLOPT_PORT, 8088); where 8088 is your real port number.

Also the proper way of handling the SSL requests involves setting the CURLOPT_CAINFO parameter:

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_CAINFO, getcwd() . "/folder/your-secure-certificate.crt");

This ensures that not just any server certificate will be trusted by your cURL session.

For example, if an attacker were to somehow redirect traffic from api.example.com to their own server, the cURL session here would not properly initialize, since the attacker would not have access to a server certificate (i.e. would not have the private key) trusted by the CA we added.

EDIT:

Many hosting companies don't allow connections "back" to your own host from your own PHP programs so perhaps that's what's happening here. Also try to apply the proxy settings, which is used in your LAN and see if it works, add this to your code if necessary:

$proxy = '127.0.0.1:8888';

curl_setopt($curl, CURLOPT_PROXY, $proxy);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top