简体   繁体   English

PHP:在cURL GET请求之后立即发出cURL POST请求

[英]PHP: cURL POST request right after cURL GET request

I'm trying to do a cURL POST request right after my first cURL request. 我正在尝试在第一次cURL请求后立即执行cURL POST请求。 I'm new with PHP and server side. 我是PHP和服务器端的新手。

My first request is a GET. 我的第一个请求是GET。 With API response i'm doing a if error -> curl_close or else -> new cURL POST request . 使用API​​响应我正在做一个if错误 - > curl_close 或者 - >新的cURL POST请求 But the second request return nothing. 但第二个请求什么也没有回复。 I've tried with a "traditionnal Postman JSON request" and the request works, so the problem is from my PHP code. 我尝试过“传统的Postman 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
  }
  }   

My GET request work and i'm able to return what i want. 我的GET请求工作,我能够返回我想要的东西。 But it seems that my second cURL request is not send, it don't return any response. 但似乎我的第二个cURL请求没有发送,它不会返回任何响应。 Is the way i'm doing or thinking it is false? 我正在做的事情还是认为它是假的? Or did I miss something? 还是我错过了什么?

Thanks 谢谢

You can try it by do initialization of cURL again for second request like below $curl2 = curl_init(); 您可以通过再次初始化cURL来尝试它,如下面的$curl2 = curl_init(); and use $curl2 in second request code 并在第二个请求代码中使用$ curl2

如果GET请求没问题,您可以在设置POST请求数据之前通过curl_resethttps://www.php.net/manual/en/function.curl-reset.php )重置curl数据。

关于POSTFIELDS行的行情令人困惑的php,试试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM