1

I am trying to get a basic cURL request to work. I am running Wamp 2.5 and PHP 5.5.12. I've double checked the php.ini files and have made sure cURL was actually set up and ready to go. Here is my code:

$url = "https://www.google.com";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
$response = curl_exec($ch);

echo $status_code;

There is no error returned except "0", and if I do var_dump the result is "boolean false". What am I doing wrong?

1
  • curl_exec() should be first Commented Dec 10, 2015 at 21:04

1 Answer 1

2

Happens, a stupid mistake. Happens to all of us :-)

Try this:

<?php
$url = "https://www.google.com";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
echo $status_code;

The output is: 302

You obviously first have to execute the request before you then can get its status code.

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

3 Comments

Sorry...I mean, even if I remove the whole status_code part, its not actually executing do you know what I mean?
@CodeTadpole It does execute the request for me. The 302 is the result of the request. What makes you think it does not execute the request on your side?
Hmmm...that makes me wonder if something is indeed wrong in my setup somewhere. I even copied and pasted what you typed and status is still 0

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.