Quantcast
Channel: 神戸ホームページ制作会社ユニファースの代表ブログ
Viewing all articles
Browse latest Browse all 218

curlコマンドにuオプションのあるものをPHPで実行する方法

$
0
0

DropboxのAPIを使う機会がありまして、curl コマンドなら、以下のように書けるものをPHPでどうやって書くかではまってしまったので(ネット上にもあまり情報が無い)、記事にしました。

https://api.dropbox.com/oauth2/token -d grant_type=refresh_token -d refresh_token=xxx -u yyy:zzz

以下が、PHPで書いた関数です

function getToken(){

$ch = "https://api.dropbox.com/oauth2/token";

$parameters = array(
	"grant_type" => "refresh_token",
	"refresh_token" => "xxx",
);


$curl=curl_init();
curl_setopt($curl, CURLOPT_URL, $ch);
curl_setopt($curl, CURLOPT_USERPWD, 'yyy:zzz');
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($parameters));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_COOKIEJAR,      'cookie');
curl_setopt($curl, CURLOPT_COOKIEFILE,     'tmp');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
 
$response= curl_exec($curl);
$resp = json_decode($response, true);
curl_close($curl);

return $resp['access_token'];

}

 


Viewing all articles
Browse latest Browse all 218

Trending Articles