php异步cURL请求

时间:2015-06-12 10:20:55

标签: php curl asynchronous web

我正在使用PHP开发一个网站并使用cURL发送请求。

我有一个网站,做了一些计算,我需要得到一个回复​​。我通过cURL发送请求目前我正在做的是发送请求,等待10秒并再次发送(最多3次),如果没有"好"收到回复。如果所有请求都失败,我会将其标记为"手动修复"。

问题是我要发送一个30秒超时的请求,并且在第10秒,如果没有收到响应,发送另一个20秒超时,在第20秒发送最后一个10秒超时。这样的事可能吗?

或者如果我的当前代码仍然存在并且我每隔10秒继续发送请求,每次超时10秒,我可以在发送第二个之后继续听第一个(当我发送第一个和第二个时)第三个)?

提前谢谢!

4 个答案:

答案 0 :(得分:5)

使用blow进行异步卷曲调用

    curl_setopt($crl, CURLOPT_TIMEOUT, 1);
    curl_setopt($crl, CURLOPT_NOSIGNAL, 1);

PHP SetOpt

答案 1 :(得分:0)

使用此功能

//background excute and wait for response
private function BackgroundsendPostData($url, Array $post) {
    $data = "";
    foreach ($post as $key => $row) {
        $row = urlencode($row); //fix the url encoding
        $key = urlencode($key); //fix the url encoding                
        if ($data == "") {
            $data .="$key=$row";
        } else {
            $data .="&$key=$row";
        }
    }
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 2000);
    $result = curl_exec($ch);
    curl_close($ch);  // Seems like good practice
    return;
}
//usage 
BackgroundsendPostData("http://www.google.co.uk/",array('pram1'=>'value1','pram2'=>'value2'));

答案 2 :(得分:0)

如果您要并行发送多个curl请求, curl_multi 将很有帮助,如下所示:

O(k + (n-k)log(k))

您可以自定义此代码。使用循环创建卷曲请求等。

Ref:scss docs

答案 3 :(得分:0)

您可以尝试使用 PHP Simple Curl Wrapper - https://github.com/Graceas/php-simple-curl-wrapper。该库允许异步处理多个请求。

向作曲家添加要求:

"require": {
    ...
    "graceas/php-simple-curl-wrapper": "v1.5.4"
    ...
}

发起请求:

$requests = [
    (new \SimpleCurlWrapper\SimpleCurlRequest())
        ->setUrl('http://ip-api.com/json?r=1')
        ->setMethod(\SimpleCurlWrapper\SimpleCurlRequest::METHOD_GET)
        ->setHeaders([
            'Accept: application/json',
            'User-Agent: simple curl wrapper',
        ])
        ->setOptions([
            CURLOPT_FOLLOWLOCATION => false,
        ])
        ->setCallback('loadCallback'),
    (new \SimpleCurlWrapper\SimpleCurlRequest())
        ->setUrl('http://ip-api.com/json?r=2')
        ->setMethod(\SimpleCurlWrapper\SimpleCurlRequest::METHOD_GET)
        ->setHeaders([
            'Accept: application/json',
            'User-Agent: simple curl wrapper',
        ])
        ->setOptions([
            CURLOPT_FOLLOWLOCATION => false,
        ])
        ->setCallback('loadCallback'),
];

发起回调:

function loadCallback(\SimpleCurlWrapper\SimpleCurlResponse $response) {
    print_r($response->getRequest()->getUrl());
    print_r($response->getHeadersAsArray());
    print_r($response->getBodyAsJson());
}

启动包装器并执行请求:

$wrapper = new \SimpleCurlWrapper\SimpleCurlWrapper();
$wrapper->setRequests($requests);
$wrapper->execute(2); // how many requests will be executed async