【问题标题】:posting JSON DATA and request header using cURL PHP使用 cURL PHP 发布 JSON DATA 和请求标头
【发布时间】:2018-04-24 11:08:20
【问题描述】:

这是我的 JSON 请求示例

{
    "Username":"admin",
    "Password":"root123",
   "PinCode” : "hello321"
}

我也想发布一个请求令牌,请求令牌必须作为请求标头发布。

当我成功发送了这些数据和有效令牌后,我需要收到如下的 jSON 响应,

{
            "Title": "Mr.",
            "Name": "Pushkov",
            "Age": "18"

}   

我的问题是,

如果代码点火器中的详细信息错误,我如何在 PHP cURL 中发布我上面提到的 JSON 数据和令牌并显示错误?

这是我到目前为止所做的......它没有给我我正在寻找的确切输出,我无法找到一种方法将我的令牌作为标头请求发送......

public function send_veri(){

$url='http://helloworld21.azurewebsites.net/api/member/login';

$ch = curl_init('http://localhost/apphome');


$data = array("Username" =>$this->input->post('un'), "Password"  =>$this->input->post('pw'), "PinCode" =>$this->input->post('VerificationCode'));                                                                    
$data_string = json_encode($data);   


// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 

curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
); 
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Set the url
//curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
}

运行此程序后,我只能获取用户名、密码和 PinCode。我无法在我的标头中发布请求令牌,导致我无法获得成员详细信息响应。

【问题讨论】:

标签: php json curl


【解决方案1】:

用于发送数据 -

  public function send_veri(){
      $url='http://helloworld21.azurewebsites.net/api/member/login';
      $ch = curl_init( $url );
      $bodyData=json_encode(array()); // here you send body Data
      $data = json_encode(array("Username" =>$this->input->post('un'), "Password" =>$this->input->post('pw'), "PinCode" =>$this->input->post('VerificationCode')));

                    curl_setopt( $ch, CURLOPT_POSTFIELDS, $bodyData );

                    curl_setopt( $ch, CURLOPT_HTTPHEADER,  array("headerdata: ".$data.",Content-Type:application/json"));

                    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

                    $result = curl_exec($ch);

                    curl_close($ch);
                   }

获取 Apache 服务器代码的 Header 数据 -

            $hEAdERS = apache_request_headers();
            $data = json_decode($hEAdERS['headerdata'], true);  
            echo $contentType=$hEAdERS['Content-Type'];
            echo $Username =  @trim($data['Username']);
            echo $Password =  @trim($data['Password']);
            echo $PinCode =  @trim($data['PinCode']);

它对我有用。

【讨论】:

  • 但是如何将我的请求令牌作为标头请求发布?例如,如果我的标题名称是 _header 并且值是“AsdRgfgfvd”,我该如何发布?
  • 在此处添加您的标头其他参数 - curl_setopt( $ch, CURLOPT_HTTPHEADER, array("headerdata: ".$data.",Content-Type:application/json"));
  • 例如 - 'curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Authorization-Key: 5996880420','headerdata: '.$data', 'Content-Type:application/json')); '
  • 如果我的回答解决了你的问题,你能接受吗?
  • 你的服务器是 Apache 还是 Nignx?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-14
  • 1970-01-01
  • 1970-01-01
  • 2015-12-06
  • 2022-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多