【问题标题】:cUrl request after fastcgi_finish_request() don't work with some urlsfastcgi_finish_request() 后的 cUrl 请求不适用于某些 url
【发布时间】:2023-01-16 21:47:57
【问题描述】:

我有一个 php 脚本 (7.3.33) 向 API (HereGeocodingApi) 发送请求。即使用户断开连接,我也使用 fastcgi_finish_request() 将数据保存在 MySQL 数据库中。

当我使用一些特定的 url 调用 curl_exec() 函数时,我的问题就出现了。令人惊讶的是,它在没有 fastcgi_finish_request() 的情况下工作得很好。更难理解的是,错误只发生在某些网址上,而不是所有网址上。

代码

PHP脚本看起来像这样(为示例缩短)

<?php

header('Content-Type: application/json');
ignore_user_abort(true);

// ... some stuff here before returning response

echo json_encode($processDatas);
fastcgi_finish_request();

$key = "MY_API_KEY";

// this work perfectly fine with or without fastcgi_finish_request() when :
//$adress = "2 AVENUE ANATOLE FRANCE, 91260 JUVISY SUR ORGE";

// this work without fastcgi_finish_request() but shut down otherwise when :
$adress = "3 AVENUE DU GENERAL DE GAU, 94240 L HAY LES ROSES";

$result = here_geocoderManager($key, $adress);

function here_geocoderManager($apikey, $adress){
    $response = "error";
    $datas = [
        "q" => $adress,
        "apikey" => $apikey
    ]; 
    $apiRequest = http_build_query($datas);
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "https://geocode.search.hereapi.com/v1/geocode?" . $apiRequest);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    try{
        // with this line replace by >> $curlResponse=["test"]; << the script end correctly anytime
        $curlResponse = curl_exec($curl);
    }
    catch(Exception $e){
        $response = $e;
    }
    curl_close($curl);
    return $response;
}

?>

到目前为止我试过的

  1. 一个或所有的组合:

    • CURLOPT_CONNECTTIMEOUT:: curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 1);
    • CURLOPT_TIMEOUT:: curl_setopt($curl, CURLOPT_TIMEOUT, 5);
    • CURLOPT_HTTPHEADER:: curl_setopt($curl, CURLOPT_HTTPHEADER, $A_CUSTOM_HEADER);
  2. file_get_contents() 出现完全相同的问题

  3. curl_init()之前添加set_time_limit(20)(有效时请求响应小于1秒)

    任何想法?一些想法会有所帮助。

【问题讨论】:

  • $curlstderr = fopen("curl_log.log","ab);curl_setopt_array($curl,array(CURLOPT_VERBOSE=&gt;1,CURLOPT_STDERR=&gt;$curlstderr)); 并向我们展示日志。

标签: php fastcgi php-curl php-7.3


【解决方案1】:

我检查这个功能如何工作我发现这个ignore_user_abort(true); fastcgi_finish_request();

【讨论】:

  • 我正在深入研究紧密连接后究竟在做什么以及什么不可用
猜你喜欢
  • 2018-09-24
  • 2013-06-26
  • 1970-01-01
  • 2017-06-16
  • 1970-01-01
  • 1970-01-01
  • 2020-06-13
  • 2021-09-06
相关资源
最近更新 更多