2017-11-11 32 views
1

JSONデータとしてログインクレデンシャルとピン番号を、httpヘッダーとしてリクエストトークンを送信します。cURL PHPでJSONデータとリクエストトークン(ヘッダーに)を投稿する方法

当初は、このようなものだ

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

と私は同様トークン私リクエスト・ヘッダーを投稿する必要があります。

とリクエストがOKであれば、私は次のようにJSONレスポンスを取得する必要があります

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

} 

私はcURLのPHPでそれをやろうとしています。以下は私のコントローラコードです。

リクエストトークンを変数に保存し、ヘッダーに渡してユーザー名、パスワード、ピン番号を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

、あなたはcurl_init()にURLを設定します。また、CURLOPT_URLオプションで別のものを通過しました。 1回のcURLリクエストでこれを行うことはできません。

OAuth 2.0を使用している場合は、アクセストークンをCURLOPT_XOAUTH2_BEARERオプションに設定できます。

詳細については、PHP manual on cURL Functionsを参照してください。

関連する問題