带有Content-Disposition的PHP cURL:form-data

时间:2017-04-28 09:08:49

标签: php api curl

我对将此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;

0 个答案:

没有答案