2014-03-13 205 views
0
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到PHP cURL

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

这相当于你--insecure PARAM:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

它相当于你-d PARAM在您张贴JSON对象。

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