我完全不熟悉PHP中的cURL請求。cURL PHP新手需要一隻手
我有一個API給了我下面的信息,並希望我通過cURL發送POST請求。我已經嘗試了一些基本的cURL示例,但不知道應該如何發送附加數據。
API文檔包含以下內容:
curl https://api.23andme.com/token/
-d client_id=xxx \
-d client_secret=yyy \
-d grant_type=authorization_code \
-d code=zzz \
-d "redirect_uri=https://localhost:5000/receive_code/"
-d "scope=basic%20rs3094315"
這裏是我的示例代碼:
$data = array(
"client_id" => $client_id,
"client_secret" => $client_secret,
"grant_type" => "authorization_code",
"code" => $code,
"redirect_uri" => "http://localhost/23andme/",
"scope" => "basic"
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch); //Uncomment to make it live again
if (!$response)
{
return false;
}
echo json_decode($response);
非常感謝。我還需要通過SSL提出請求。它正在工作。 –
您離開'CURLOPT_HTTPHEADER'並且'CURLOPT_POST'是一個類似於其他的布爾值,而不是後置參數的計數。你也不能從if語句中echo出一個對象'echo json_decode(..)'.. AND'return false;'?看起來像一個屠殺的複製和粘貼。 –