duanqian6982 2017-11-11 08:39
浏览 55

使用cURL PHP发布JSON DATA和请求标头

This is my sample JSON Request

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

And also I want to post a request token as well, the request token has to be posted as a Request Header.

When I successfully sent those data and valid token, I need to receive a jSON response as below,

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

}   

My Question is,

How can I POST my above mentioned JSON data and the token in PHP cURL and display an error if the details are wrong in code igniter?

This is what I have done so far..and its not giving me the exact output im looking for and I couldn't figure out a way to send my token as a header request...

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);
}

once I run this, I could only get Username, Password and the PinCode only. I cannot post the request token in my header, in causing of that i cannot get the member detail response.

  • 写回答

1条回答 默认 最新

  • dty9731 2017-11-11 09:08
    关注

    For Sending Data -

      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);
                       }
    

    Get Header data on Apache Server Code -

                $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']);
    

    It works for me.

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)