首页 > 解决方案 > 带有 httpcode 响应的 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);


?>

请问各位大神,这种情况怎么办?

谢谢你们 。

标签: phpcurl

解决方案


我认为您只是错误地命名了一个变量。你正在检查$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);

?>

推荐阅读