1

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}
Lasith
  • 565
  • 1
  • 6
  • 17
  • 2
    Start here - http://php.net/curl, then [here](http://docs.guzzlephp.org/en/latest/). Also read over [how to ask a question](http://stackoverflow.com/help/how-to-ask) – Aaron W. Jun 23 '16 at 12:56
  • U need this (: https://incarnate.github.io/curl-to-php/ – Walk May 19 '17 at 10:07

3 Answers3

2

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:

Álvaro Guimarães
  • 2,154
  • 2
  • 16
  • 24
0
<?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);
}
?>

link

Community
  • 1
  • 1
Maksym Semenykhin
  • 1,924
  • 15
  • 26
-1

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";

}

  1. PHP CURL GET EXAMPLE:- You can use the below code to send GET request.

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");