0

I want to get curl response using the following code :

$url = "http://google.co.in";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    $result=curl_exec ($ch);
    if(curl_exec($ch) === false) {
        print_r($result);
        echo "ok";
    }
    else {
        echo "error";
    }

    curl_close ($ch);

But when I am executing the code then getting response 'OK' but not printing the value of result. What I am missing ?

Rohitashv Singhal
  • 4,517
  • 13
  • 57
  • 105
  • What's wrong with printing OK? You need to tell us what you're missing by the way. The way you ask it, it's hard to tell unless you tell us because only you know the answer of personal questions. So please turn this into a programming question. And can you add reference which exisitng material that is close didn't do it for you and why? – hakre Dec 08 '15 at 06:34
  • I want to print the html of the given url but if I use the code than it should print result but printing only OK – Rohitashv Singhal Dec 08 '15 at 06:36
  • And you think the code should do that only by you wanting it to do? Or the other way round: If you don't want that, would the code solve your problem then? What I'm trying to say here: The code does exactly what you commanded it to do. Wanting alone is only helpful when it results into concrete commands in that code. So please outline *each* of the commands you present there and explain how they fullfill your wanting (or not). – hakre Dec 08 '15 at 06:36
  • hint #1: You're calling `curl_exec` twice. The second time normally returns `FALSE` always. `$result` is of the first time which *might* be `TRUE` however must not be the HTML you're looking for. The HTML will be output to [*standard output*](https://en.wikipedia.org/wiki/Standard_streams) unless you tell curl to return the response body with [`curl_exec`](http://php.net/curl_exec) by making use of the [option `CURLOPT_RETURNTRANSFER` constant](http://stackoverflow.com/a/24050449/367456). Please see the many duplicate Q&A material we have on site already, thanks! – hakre Dec 08 '15 at 09:18

2 Answers2

0

here you are:

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, "http://www.google.co.in"); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
$result = curl_exec($ch); 
curl_close($ch);
if(!$result) {
    print_r($result);
    echo "ok";
}
else {
    echo "error";
}
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • the condiition `if(!$result) {` is also fulfilled when the answer is correct and empty (`$result === ''`). Although it does not make big sense it may mean something (example - 0 records available). – koral Dec 08 '15 at 07:35
  • it doesn't make sense in this condition too: if(curl_exec($ch) === false) { – RomanPerekhrest Dec 08 '15 at 07:45
  • yes - you are right; I fixed my answer – koral Dec 08 '15 at 12:27
0

Get error of last operation http://php.net/manual/en/function.curl-error.php

$result = curl_exec($ch);
if($result === false) {
    $errorMsg = curl_error($ch);
    echo $errorMsg;
    //your code
    echo "\nok";
} else {
    echo $result;
}

Then you can try to fix the error.

koral
  • 2,807
  • 3
  • 37
  • 65