Curl PHP as transmitted in the heading simultaneously json and data



  • curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

    With CUrl's request, I'm sending json, but I also need to get the token. In the usual case, I'd give it to the mass, but application/json and there's a mass. Question: How am I to share this parameter and token in a format: "mytoken": $token



  • You can do that.

    $arFields = [
       "name" => 'Иван Иванов',
       "birthday" => '02.02.2002',
       "phone" => '+7(999)99-99-99',
       "sex" => 'Male',
    ];
    $jFields = json_encode($arFields, JSON_UNESCAPED_UNICODE);
    

    $arOptions = [
    CURLOPT_SSL_VERIFYPEER => false, //Проверка SSL сертификата
    CURLOPT_SSL_VERIFYHOST => false, //Проверка хоста на соответствие с SSL
    CURLOPT_HEADER => true, //Включаем передачу заголовка
    CURLOPT_RETURNTRANSFER => true, //Возврат результата
    CURLOPT_HTTPHEADER => [
    'Authorization: Bearer ' . base64_encode($token),
    'Accept: application/json',
    'Content-Type: application/json'
    ], //Массив заголовков
    CURLOPT_URL => $this->url . '/client/' . $id, //Загружаемый URL, куда посылаем
    CURLOPT_POST => true, //Передаем данные POST
    CURLOPT_POSTFIELDS => $jFields, //POST - запрос
    ];
    curl_setopt_array($ch, $arOptions);



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2