curl -H 'content-type: application/json' --insecure -d '{"client_id":"w44p0d00.apps.2do2go", "client_secret":"mvlldlsfKLLSczxc12Kcks910cccs", "grant_type":"client_credentials", "scope": "anonymous"}' https://someurl.com/oauth/token
此命令行cURL完美运行。我如何在PHP中做同样的事情?
curl_setopt($ch, CURLOPT_URL, 'https://someurl.com/oauth/token'); //this my url
curl_setopt($ch, CURLOPT_HTTPHEADER, array('content-type: application/json')); //its -H
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
答案 0 :(得分:1)
这相当于您的--insecure
参数:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
它等同于您发布json对象的-d
参数。
$json = '{"client_id":"w44p0d00.apps.2do2go", "client_secret":"mvlldlsfKLLSczxc12Kcks910cccs", "grant_type":"client_credentials", "scope": "anonymous"}';
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
将这些添加到现有卷曲后,执行以下操作以执行卷曲并打印数据:
$response = curl_exec($ch);
curl_close($ch);
print $response;
答案 1 :(得分:0)
$url = 'https://someurl.com/oauth/token';
$fields = array(
'client_id' => urlencode("w44p0d00.apps.2do2go"),
....
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('content-type: application/json'));
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);