0

I'm trying to execute a cURL request in PHP to my localhost, but I get the following error:

cURL error (7): Failed to connect to localhost port 8000: Connection refused

I've tried to Google this message, and also found similar questions on StackOverflow, but nothing seems to be working.

curl.php:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "localhost:8000/test.php");
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
$response = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'cURL error (' . curl_errno($ch) . '): ' . curl_error($ch);
} else {
    echo $response;
}

curl_close($ch);

test.php:

echo json_encode(array(
    "message" => "Hello World!"
));

If I remove the "CURLOPT_IPRESOLVE" value, the request never resolves. I've also tried setting both "CURLOPT_SSL_VERIFYPEER" and "CURLOPT_SSL_VERIFYHOST" to false. Still not working.

But if I navigate to localhost:8000/test.php in my browser, I get the correct response. I can also use the "curl localhost:8000/test.php" command and receive the correct response

Thanks for the help!

1 Answers1

-1

You are missing the http:// in the begining of the request like Rahul Prajapati wrote you in a comment

see this other post PHP + curl, HTTP POST sample code?

note this line uses curl_setopt($ch, CURLOPT_URL,"http://www.example.com/tester.phtml");

the thing is, if you put in your browser localhost:8000/test.php, the browser will put the protocol by default and it will appear that you are making the same call and getting diferents outputs

Hope this helps you