php - 从 PHP 中的多个 cURL 返回 http 状态代码

标签 php curl http-status-codes curl-multi

我正在尝试从我的脚本发出的两个并行 cURL 请求的 header 信息中返回两个 http 状态代码。到目前为止,我的脚本如下所示,末尾的 print_r() 打印出:Array ( [0] => 200 [1] => )。我不确定为什么它不返回第二个状态代码?提前致谢。

function checkHTTPStatusCode($ip1,$ip2) {
    $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";

    //create cURL resources
    $ch1 = curl_init();
    $ch1 = curl_init();

    //set opptions
    curl_setopt ($ch1, CURLOPT_URL,$ip1);
    curl_setopt ($ch1, CURLOPT_USERAGENT, $agent);
    curl_setopt ($ch1, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch1, CURLOPT_VERBOSE, false);
    curl_setopt ($ch1, CURLOPT_TIMEOUT, 5);

    curl_setopt ($ch2, CURLOPT_URL,$ip2);
    curl_setopt ($ch2, CURLOPT_USERAGENT, $agent);
    curl_setopt ($ch2, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch2, CURLOPT_VERBOSE, false);
    curl_setopt ($ch2, CURLOPT_TIMEOUT, 5);

    //create the multiple cURL handle
    $mh = curl_multi_init();

    //add the two handles
    curl_multi_add_handle($mh,$ch1);
    curl_multi_add_handle($mh,$ch2);

    //execute the handles
    $running = null;
    do {
        curl_multi_exec($mh, $running);
    } while($running > 0);

    //get http status codes
    $httpcode1 = curl_getinfo($ch1, CURLINFO_HTTP_CODE);
    $httpcode2 = curl_getinfo($ch2, CURLINFO_HTTP_CODE);

    //close the handles
    curl_multi_remove_handle($mh, $ch1);
    curl_multi_remove_handle($mh, $ch2);
    curl_multi_close($mh);  

    return array($httpcode1,$httpcode2);
}

$test = checkHTTPStatusCode('http://www.yahoo.com','http://www.google.com');

print_r($test);

最佳答案

此处输入错误:

//create cURL resources
$ch1 = curl_init();
$ch1 = curl_init();

我猜你的意思是 $ch2 = curl_init(); 第二个。

关于php - 从 PHP 中的多个 cURL 返回 http 状态代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15769046/

相关文章:

php - 选择 DISTINCT 但回显全部?

curl - 在 PostMan 中模拟特定的 CURL

c - 如何在C中使用curl获取响应值

php - 如何使用 php curl 获取网页并显示该网页 html?

c# - System.Net.WebException HTTP 状态码

http - 创建但不返回数据的端点的状态代码

php - 如何使用 Zend Framework 在用户之间实现共享变量?

php - 在包含 PHP 变量的 HTML 字符串中转义引号的正确方法

api - 如果路径参数无效,我应该使用什么 HTTP 状态响应代码?

php - 从命令行进行快速基准测试