How do I make calls to the linkedin api using guzzle? So far I've tried
$client = new \GuzzleHttp\Client(['base_uri' => 'https://api.linkedin.com']);
$access_token = 'the_access_token';
$req = $client->request('POST', '/v1/people/~?format=json', [
'headers' => ["Authorization" => "Bearer " . $access_token,
"Content-Type" => "application/json", "x-li-format"=>"json"],
'client_id' => 'the_client_id',
'client_secret' => 'the_client_secret',
'connection' => 'Keep-Alive'
]);
dd($req);
but I just get an error that reads:
Client error: POST https://api.linkedin.com/v1/people/~?format=json resulted in a 405
Im working with laravel 5.1 and guzzle 6.2.
Silly mistake simply had to change $client->request('POST', from POST to GET so it would be:
$client = new \GuzzleHttp\Client(['base_uri' => 'https://api.linkedin.com']);
$access_token = 'the_access_token';
$req = $client->request('GET', '/v1/people/~?format=json', [
'headers' => ["Authorization" => "Bearer " . $access_token,
"Content-Type" => "application/json", "x-li-format"=>"json"],
'client_id' => 'the_client_id',
'client_secret' => 'the_client_secret',
'connection' => 'Keep-Alive'
]);
dd($req);
I also decoded the response which makes it readable by using json_decode($request->getBody()).
Hope it helps if someone else gets stuck on the same problem.
Related
I use laravel and GuzzleHttp for send post request same as following
$client = new Client(['verify' => false]);
$data = [
'headers' => [
'Authorization' => 'code',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'form-params' => [
'redirect_uri' => 'http://localhost',
'code' => 'MZTADnFF6m'
]
];
$response = $client->request('POST', 'https://oom.com', $data);
but my problem is 'http://localhost' send as 'http:\/\/localhost' to api server
how can i fix this problem?
You can use stripslashes() for achieve this. You can try
stripslashes("http:\/\/localhost");
it will resultd in
http://localhost
as a result
use urlencode
here it is
'redirect_uri' => urlencode('http://localhost'),
I have this post:
{"latitude":"","longitude":"","countryCode":"ES","filterPostalCode":"","filterCity":"","filterCountryCode":"ES","searchText":"","nextPageToken":0,"storeType":"normal","checkStoreAvailability":false}
And i'm trying to send it like this on Guzzle 6.0+
'headers' => [
'Content-Type' => 'application/json',
'Referer' => 'https://www.rituals.com/es-es/stores'],
'body' => '{"latitude":"","longitude":"","countryCode":"ES","filterPostalCode":"","filterCity":"","filterCountryCode":"ES","searchText":"","nextPageToken":0,"storeType":"normal","checkStoreAvailability":false}']
But it's not working, any way to send everything without formating it like I posted? thanks!
First of create Client object
$client = new Client([
'http_errors' => false,
'verify' => false,
]);
And then your request with params
$response = $client->request($requestMethod, $url, array_merge(
['json' => $body],
['headers' => $headers]
));
We're moving from Guzzle3 to Guzzle6. I wrote a process to authenticate and access the Azure Management API that worked fine in Guzzle3. However, I'm unable to figure out how to get it to work in Guzzle6. The purpose is to get the access token which is then use in subsequent requests to the Azure Management API.
Original Code:
$client = new Guzzle\Http\Client();
$request = $client->post("https://login.microsoftonline.com/{$tenant_id}/oauth2/token",
Array(
'Accept' => 'application/json',
),
Array(
'grant_type' => 'client_credentials',
'client_id' => $application_id,
'client_secret' => $application_secret,
'resource' => 'https://management.core.windows.net/',
)
);
$response = $request->send();
$body = $response->getBody(true);
New code I'm working on:
$client = new GuzzleHttp\Client();
$response = $client->request(
'POST',
"https://login.microsoftonline.com/{$tenant_id}/oauth2/token",
Array(
GuzzleHttp\RequestOptions::JSON => Array(
'grant_type' => 'client_credentials',
'client_id' => $application_id,
'client_secret' => $application_secret,
'resource' => 'https://management.core.windows.net/',
)
)
);
I've tried so many variations with no luck. I would be grateful for any insight anyone could provide.
Thanks!
Well, I guess posting on here helped guide my thoughts on this. I was able to get it to work. For anyone else in the same boat, here's the solution I came up with:
$client = new GuzzleHttp\Client();
$response = $client->request(
'POST',
"https://login.microsoftonline.com/{$tenant_id}/oauth2/token",
Array(
'form_params' => Array(
'grant_type' => 'client_credentials',
'client_id' => $application_id,
'client_secret' => $application_secret,
'resource' => 'https://management.core.windows.net/',
)
)
);
It appear Accept: application/json has not been set for the request header. I am not getting json response.
$params = [
'client_id' => 'xxxx',
'client_secret' => 'xxxxxxxxxx',
'code' => $request->get('code'),
'state' => $request->get('state'),
];
$client = new Client(['headers' => ['Accept: application/json']]);
$response = $client->post($tokenUrl, [
'form_params' => $params,
]);
echo $response->getBody();
How do I solve this?
You should write the headers as an associative array, according to http://docs.guzzlephp.org/en/latest/request-options.html.
So, try
$client = new Client(['headers' => ['Accept' => 'application/json']]);
I'm trying to request this way:
$body = [];
$body['holder_name'] = $full_name;
$body['bank_code'] = $bank_number;
$body['routing_number'] = $branch_number;
$body['account_number'] = $account_number;
$body['type'] = 'checking';
$client = new GuzzleHttp\Client([
'base_url' => [$url, []],
'headers' => ['content-type' => 'application/json', 'Accept' => 'application/json'],
'defaults' => [
'auth' => [$publishable_key, ''],
],
'body' => json_encode($body),
]);
The problem is that this request is being set without Content-Type.
What am I doing wrong?
Ok .. the problem was that I was setting body and headers outside of defautls. the solution is:
$client = new GuzzleHttp\Client([
'base_url' => [$url, []],
'defaults' => [
'auth' => [$publishable_key, ''],
'headers' => ['content-type' => 'application/json', 'Accept' => 'application/json'],
'body' => json_encode($body),
],
]);
Guzzle 6
Guzzle will set the Content-Type header to
application/x-www-form-urlencoded when no Content-Type header is
already present.
You have 2 options.
Option 1: On the Client directly
$client = new GuzzleHttp\Client(
['headers' => [
'Content-Type' => 'application/json'
]
]
);
Option 2: On a Per Request basis
// Set various headers on a request
$client = new GuzzleHttp\Client();
$client->request('GET', '/whatever', [
'headers' => [
'Content-Type' => 'application/json'
]
]);
You can refer to Guzzle 6: Request Options
I was encountering the same issue with the Hubspot API that requires to set application/json as Content-Type for POST requests.
I fixed it this way
$client = new Client([
'base_uri' => 'https://api.hubapi.com/',
'timeout' => 5,
'headers' => ['Content-Type' => 'application/json']
]);
And then performing my requests the regular way
try
{
$response = $client->request('POST', '/contacts/v1/contact/email/test#test.com/profile',
['query' => MY_HUBSPOT_API_KEY, 'body' => $body]);
}
catch (RequestException $e) { print_r($e); }
I hope this helps.