2017-10-16 59 views
0

你好,我想知道如何用PHP來模擬curl命令。我想模擬以下:如何用PHP模擬curl -X帖子

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' 
    )); 

,但沒有運氣。

+7

[PHP +捲曲,HTTP POST示例代碼η]爲可能的複製(https://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code) – Daan

+2

基本谷歌搜索給你答案... – d3L

回答

0

使用http_build_query將數據編碼爲查詢字符串,然後設置CURLOPT_POSTFIELDS選項,以CURLOPT_POSTCURLOPT_URL PARAMS一起,並最終將其發送。

$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);