How do I send following curl request with php
$ curl -X POST -u "client_id:secret" https://bitbucket.org/site/oauth2/access_token
-d grant_type=authorization_code -d code={code}
How do I send following curl request with php
$ curl -X POST -u "client_id:secret" https://bitbucket.org/site/oauth2/access_token
-d grant_type=authorization_code -d code={code}
Use GuzzleHttp.
<?php
$client = new GuzzleHttp\Client();
$response = $client->request('POST', 'https://bitbucket.org/site/oauth2/access_token', [
'auth' => ['client_id', 'secret'],
'form_params' => [
'grant_type' => 'authorization_code',
'code' => 'code'
]
]);
$code = $response->getStatusCode(); // must be 200
$reason = $response->getReasonPhrase(); // must be OK
$body = (string) $response->getBody(); // must have your data
Attention, if you are implementing an OAuth Client, I strongly recommend using an opensource library:
<?php
if ($curl = curl_init()) {
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_URL, 'http://mysite.ru/');
curl_setopt($curl, CURLOPT_USERPWD, "client_id:secret");
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "code=someCode&");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($curl);
curl_close($curl);
}
?>
step 1) Initialize CURL session
$ch = curl_init();
step 2) Provide options for the CURL session
curl_setopt($ch,CURLOPT_URL,"http://akshayparmar.in");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
//curl_setopt($ch,CURLOPT_HEADER, true); //if you want headers
CURLOPT_URL -> URL to fetch
CURLOPT_HEADER -> to include the header/not
CURLOPT_RETURNTRANSFER -> if it is set to true, data is returned as string instead of outputting it.
For full list of options, check PHP Documentation.
step 3).Execute the CURL session
$output=curl_exec($ch);
step 4). Close the session
curl_close($ch);
Note: You can check whether CURL enabled/not with the following code.
if(is_callable('curl_init'))
{
echo "Enabled";
}
else
{
echo "Not enabled";
}
function httpGet($url) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$output=curl_exec($ch);
curl_close($ch);
return $output;
}
echo httpGet("http://akshayparmar.in");