我想知道如何在curl
中模拟PHP
命令。我想模拟这个:
curl -X POST https://example.com/token\?\
client_id\=your_client_id\&\
client_secret\=your_client_secret\&\
grant_type\=client_credentials\&\
scope\=public
我试过了:
curl_setopt($s, CURLOPT_POST,array(
'client_id=my_id',
'client_secret/=my_secred',
'grant_type/=client_credentials',
'scope/=public'
));
但我没有运气。
答案 0 :(得分:1)
使用http_build_query
将数据编码为查询字符串,然后设置CURLOPT_POSTFIELDS
选项以及CURLOPT_POST
和CURLOPT_URL
参数,最后发送。< / p>
$s = curl_init();
curl_setopt($s, CURLOPT_URL, 'https://example.com/token');
curl_setopt($s, CURLOPT_POST, 1);
curl_setopt($s, CURLOPT_POSTFIELDS, http_build_query([
'client_id' => 'my_id',
'client_secret' => 'my_secred',
'grant_type' => 'client_credentials',
'scope' => 'public'
]));
curl_exec($s);
curl_close($s);