3

我正在尝试检查 cURL 的输出。

<?php

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://blablabla.com");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $curlresult=curl_exec ($ch);
      curl_close ($ch);



    if ($curlresult == "OK") {
        $result = "The curl action was succeeded! (OUTPUT of curl is: ".$curlresult.")";
    } else {
        $result = "The curl action has FAILED! (OUTPUT of curl is: ".$curlresult.")";
    }

echo $result;

?>

URL (https://blablabla.com) 是一个显示 OK 的 URL。所以,使用代码,我希望看到

“卷曲动作成功!(卷曲的输出为:OK)”

但是,我得到的是:

卷曲动作失败了!(卷曲的输出是:OK)

我想我犯了一些愚蠢的错误。如何检查https://blablabla.com是否包含“OK”?

谢谢!

4

4 回答 4

5

You may be getting extra white spaces before or after the OK or other characters.

I suggest doing what the people above suggested by testing what is exactly inside the array with var_dump($curlresult); or print_r($curlresult);

But alternatively you could instead of matching that $curlresult equals only "OK", you could test if $curlresult contains "OK" inside of it.

<?php

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://blablabla.com");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $curlresult=curl_exec ($ch);
      curl_close ($ch);



    if (preg_match("/OK/i", $curlresult)) {
        $result = "The curl action was succeeded! (OUTPUT of curl is: ".$curlresult.")";
    } else {
        $result = "The curl action has FAILED! (OUTPUT of curl is: ".$curlresult.")";
    }

echo $result;

?>
于 2012-06-23T03:42:09.507 回答
2

you can do

$info = curl_getinfo($ch);
var_dump($info);

gives you info on http status code returned and connect time etc.

于 2012-06-22T17:49:04.057 回答
1

您应该尝试var_dump($curlresult);看看您真正得到了什么,否则我认为使用 SSL (HTTPS) 可能存在问题,以快速解决此问题(接受任何服务器证书):

<?php

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://blablabla.com");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $curlresult=curl_exec ($ch);
     curl_close ($ch);



    if ($curlresult == "OK") {
        $result = "The curl action was succeeded! (OUTPUT of curl is: ".$curlresult.")";
    } else {
        $result = "The curl action has FAILED! (OUTPUT of curl is: ".$curlresult.")";
    }

//var_dump($curlresult);
echo $result;

?>
于 2012-06-22T17:22:42.907 回答
0

看,在这个代码块中:

if ($curlresult == "OK") {
        $result = "The curl action was succeeded! (OUTPUT of curl is: ".$curlresult.")";
    } else {
        $result = "The curl action has FAILED! (OUTPUT of curl is: ".$curlresult.")";
    }

it is assumed that $curlresult is string, First try var_dump($curlresult) to know what datatype response is returned from this call. May be return type is not string.
Then use the check for that datatype in if condition.

于 2012-06-22T17:29:18.667 回答