【问题标题】:PHP: cURL POST request right after cURL GET requestPHP:在 cURL GET 请求之后立即发出 cURL POST 请求
【发布时间】:2019-08-08 13:12:39
【问题描述】:

我正在尝试在我的第一个 cURL 请求之后立即执行 cURL POST 请求。 我是 PHP 和服务器端的新手。

我的第一个请求是 GET。通过 API 响应,我正在执行 if error -> curl_close else -> new cURL POST request。但是第二个请求什么也没返回。我尝试过使用“传统邮递员 JSON 请求”并且请求有效,所以问题出在我的 PHP 代码中。

/* First we try to have id user from his mail*/

$curl = curl_init();   
$usermail = "user@mail.com";

//url = url + email
$gen_url = "https://myendpoint.com/manage/v1/user?search_text=";
$url = $gen_url.$usermail;

//GET request return all info about user, we do this to have his userid 
curl_setopt_array($curl, array(
  CURLOPT_URL => $url, 
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "Authorization: Bearer $tokenDecode",
    "Content-Type: application/x-www-form-urlencoded",
  ),
));

$response2 = curl_exec($curl);
$err = curl_error($curl);

if ($err) {
  echo "cURL Error #:" . $err;
  curl_close($curl);
} else {
  $userInfo = json_decode($response2, true);
  $userId = $userInfo["data"]["items"][0]["user_id"] . "<br>"; 

  /* We have the user_id, we can POST on user's additionnal fields */

  $gen_url2="https://myendpoint.com/manage/v1/user/";   
  $url2= $gen_url2.$userId;

  curl_setopt_array($curl, array(
    CURLOPT_URL => $url2,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "PUT",
    CURLOPT_POSTFIELDS => "{'additional_fields':[{'id':98,'value':2}]",
    CURLOPT_HTTPHEADER => array(
      "Authorization: Bearer $tokenDecode",
      "Content-Type: application/json",
    ),
  ));

  $response3 = curl_exec($curl);
  $err2 = curl_error($curl);

  curl_close($curl);

  if ($err2) {
    echo "cURL Error #:" . $err2;
  } else {
    echo $response3; //Show nothing
  }
  }   

我的 GET 请求工作,我能够返回我想要的。 但似乎我的第二个 cURL 请求没有发送,它没有返回任何响应。 我正在做或认为的方式是错误的吗?还是我错过了什么?

谢谢

【问题讨论】:

  • 显示的代码不能工作,CURLOPT_POSTFIELDS =&gt; "{"additional_fields":[{"id":98,"value":2}]"显然是语法错误。
  • CURLOPT_POSTFIELDS =&gt; json_encode(["additional_fields" =&gt; [ ["id":98,"value":2] ] ]),

标签: php curl


【解决方案1】:

如果 GET 请求正常,您可以在为 POST 请求设置数据之前通过 curl_reset (https://www.php.net/manual/en/function.curl-reset.php) 重置 curl 数据。

【讨论】:

    【解决方案2】:

    您可以通过再次初始化 cURL 来尝试第二个请求,如下所示 $curl2 = curl_init(); 并在第二个请求代码中使用 $curl2

    【讨论】:

      【解决方案3】:

      POSTFIELDS 行中的引号让 php 感到困惑,试试这个

        CURLOPT_POSTFIELDS => "{'additional_fields':[{'id':98,'value':2}]",
      

      【讨论】:

      • 谢谢怀疑,我已经修改了。它会减少混乱。但问题仍然存在。
      • 那你肯定有另一个问题。试试php -l yourfile.php - 有什么错误吗?
      猜你喜欢
      • 1970-01-01
      • 2013-06-18
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 2020-09-07
      • 1970-01-01
      • 2016-02-22
      • 2015-12-06
      相关资源
      最近更新 更多