2017-11-11 73 views
1

我想發送登錄憑證和一個PIN碼作爲JSON數據和請求令牌作爲我的http頭。如何在cURL中發佈JSON數據和請求令牌(在標題中)php

最初是這樣的,

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

,我需要我的張貼要求頭令牌爲好。

,如果該請求是好的,我應該得到JSON響應如下,

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

} 

我試圖做到這一點在PHP捲曲。以下是我的控制器代碼。

我試圖將我的請求令牌保存到一個變量,並將其傳遞到標題中,並將用戶名,密碼和pinnumber作爲JSON數據發送。如果登錄成功,則顯示用戶信息。但我正努力從這裏前進。我怎樣才能做到這一點?

public function send_data() { 

$url='http://samplesite.azurewebsites.net/api/member/loginmember'; 

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


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


//echo $data_string; 

// Disable SSL verification 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
// Will return the response, if false it print the response 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 

curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 

curl_setopt($ch, CURLOPT_HTTPHEADER, array(                   
    'Content-Type: application/json',                     
    'Content-Length: ' . strlen($data_string))                  
); 
// Set the url 
curl_setopt($ch, CURLOPT_URL,$url); 
// Execute 
$result=curl_exec($ch); 


//var_dump(json_decode($result, true)); 
$data2=json_decode($result, true); 
$ref_id = ((is_array($data2["UserCode"]) ? implode(", ", $data2["PinCode"]) : $data2["RequestToken"])); // array to string 
$acc_t = $data2["RequestToken"]; 
$uun = $data2["UserCode"]; 

//echo $acc_t; 


// Closing 
curl_close($ch); 
//print $result ; 
} 

回答

0

在你上面的代碼,你設置的URL curl_init(),你也通過另外一臺CURLOPT_URL選項。你不能在一個cURL請求中這樣做。

如果您使用OAuth 2.0,則可以將訪問令牌設置爲CURLOPT_XOAUTH2_BEARER選項。

更多信息請參考PHP manual on cURL Functions

相關問題