我完全不熟悉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;'?看起来像一个屠杀的复制和粘贴。 –