带有httpcode响应的PHP CURL条件

时间:2018-11-19 01:39:56

标签: php curl

我正在尝试在我的PHP CURL代码上添加一个条件,以继续执行curl,直到由于网站流量高而导致网关响应不佳而导致httpcode = 200时,如果获取httpcode 200则执行一些指令。 >

所以我正在尝试我的代码,但这没有帮助。

    <?php

    $ch = curl_init("https://example.com/index.php");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt($ch, CURLOPT_PROXY, '151.80.143.155:53281');
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'C:\AppServ\www\cloud\cookie.txt');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.53 Safari/537.36');
    curl_exec($ch);

    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        while(true){
            if($http !== 200){
                curl_exec($ch);
            curl_close($ch);
            sleep(8);
            }

            else{
            // Do some instruction ...
        sleep(8);
        }
        }

    curl_close($ch);


?>

请大家我该怎么做?

谢谢你们。

1 个答案:

答案 0 :(得分:0)

我认为您只是错误地命名了一个变量。您正在检查$http,而应检查$httpcode

<?php

$ch = curl_init("https://example.com/index.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_PROXY, '151.80.143.155:53281');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'C:\AppServ\www\cloud\cookie.txt');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.53 Safari/537.36');
curl_exec($ch);

$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
while(true){
    if($httpcode !== 200){ // This line was wrong
        curl_exec($ch);
        curl_close($ch);
        sleep(8);
    } else{
        // Do some instruction ...
        sleep(8);
        break;
    }
}

curl_close($ch);

?>