简体   繁体   English

命令行cURL到PHP cURL

[英]command line cURL to PHP cURL

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

This command line cURL works perfectly. 此命令行cURL可以完美运行。 How I can do the same in PHP? 我如何在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);

This is equivalent to your --insecure param: 这等效于--insecure参数:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Its equivalent to your -d param at where you are posting json object. 它等效于您在其中发布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);

After adding these to your existing curl, do the below to perform the curl and print the data: 将它们添加到现有的卷发之后,请执行以下操作以执行卷发并打印数据:

$response = curl_exec($ch);
curl_close($ch);
print $response;
$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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM