天天看点

php的curl请求带form-data,带有Content-Disposition的PHP cURL:form-data

我将这个Instagram cURL转换成PHP看起来很迷惑,如下所示:

curl -F 'client_id=CLIENT_ID' \

-F 'client_secret=CLIENT_SECRET' \

-F 'grant_type=authorization_code' \

-F 'redirect_uri=AUTHORIZATION_REDIRECT_URI' \

-F 'code=CODE' \

https://api.instagram.com/oauth/access_token这是我当前的代码,有一个错误

{

"error_type": "OAuthException",

"code": 400,

"error_message": "You must provide a client_id"

}带样本值的当前代码:

$client_id = "7d60c47cdwe249069cdb07a0f28e19cb";

$client_secret = "de5awwe3ebf1443d8dcf0fbc88e5ab1b";

$grant_type = "authorization_code";

$redirect_uri = "http://sexample.net/create-code.php";

$code ="y23d86c19b4c3b0a8b561e64c93f2b3f";

$ch = curl_init();

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

//format URL to grant access to email and sitename passed

$data = '{"client_id":"'.$client_id.'","client_secret":"'.$client_secret.'","grant_type":"'.$grant_type.'","redirect_uri":"'.$redirect_uri.'","code":"'.$code.'"}';

curl_setopt($ch, CURLOPT_URL, 'https://api.instagram.com/oauth/access_token');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

//execute cURL call and get template data

$output = curl_exec($ch);

curl_close($ch);

echo $output;