I need to request an api with curl in PHP.
First of all I need to get a token access, and to get it I need to request to this url (with my "client_id") : https://testexellia.bitrix24.fr/oauth/authorize/?response_type=code&client_id=local.5d3c9f79b65439.23207420&redirect_uri=app_URL
It should answer me a new url with the token inside.
It works very well when I request with Chrome, but with curl I get this answer -> https://www.bitrix24.net/oauth/authorize/?user_lang=fr&client_id=b24.5d399107d0f2b9.16511658&redirect_uri=https%3A%2F%2Ftestexellia.bitrix24.fr%2Foauth%2Fauthorize%2F%3Fauth_service_id%3DBitrix24Net%26oauth_proxy_params%3DcmVkaXJlY3RfdXJpPWFwcF9VUkwmY2xpZW50X2lkPWxvY2FsLjVkM2M5Zjc5YjY1NDM5LjIzMjA3NDIwJnJlc3BvbnNlX3R5cGU9Y29kZQ%253D%253D%26sessid%3Db6ded2712b7e66e4b34f05411eb2f4f1&scope=auth,profile&response_type=code&mode=page&state=site_id%3Ds1%26backurl%3D%252Foauth%252Fauthorize%252F%253Fcheck_key%253D7d6109a54034f59c406798d6a9667a9c%2526response_type%253Dcode%2526client_id%253Dlocal.5d3c9f79b65439.23207420%2526redirect_uri%253Dapp_URL%26mode%3Dpage
So the login page.
I know where is the problem, I'm already logged with Chrome (probably with cookies I guess) to the website, and obviously the curl request not.
I guess I need to add some headers informations to my request, like a password and login, but I'm logged to this website with my Google Account.
So is there a way to add Google informations in the curl's header ? I can't find how.
Here is the doc of the API, in case you need.
Edit : Here is the code.
$curl = curl_init();
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://testexellia.bitrix24.fr/oauth/authorize/?response_type=code&client_id=local.5d3c9f79b65439.23207420&redirect_uri=app_URL&sessid=b6ded2712b7e66e4b34f05411eb2f4f1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Authorization: Bearer 08493f5d003eabc0003e9b9e00000001f0f103a29e30c5356d8e8142404add6515e135",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Host: testexellia.bitrix24.fr",
"Postman-Token: 6de6ba41-b9fc-4541-98b0-c2f9d9b1ea7f,b36cd02a-87f8-4fb1-821c-54602a300bc5",
"User-Agent: PostmanRuntime/7.13.0",
"accept-encoding: gzip, deflate",
"cache-control: no-cache",
"cookie: PHPSESSID=fOJKQhrXBNuNmrmIOXXg0YDlIeckdVfm; qmb=.; BITRIX_SM_SALE_UID=0"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
$content = curl_exec($curl);
$redirectedUrl = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
curl_close($curl);
echo $redirectedUrl;
Thank you very much.
Benjamin.
Related
I try to create a storage account in my Azure Stack with curl in a PHP site but unfortunately I always receive a error "HTTP Error 411. The request must be chunked or have a content length.".
I have put ""Content-Length: 0,"" or the correct number but still same error.
Here is my code (who is generated by Postman) :
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://management.myazurestack.com/subscriptions/XXXXXX/resourcegroups/YYYYYYYY/providers/Microsoft.Storage/storageAccounts/sa01?api-version=2017-10-01",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => "{\r\n \"sku\": {\r\n \"name\": \"Standard_LRS\"\r\n },\r\n \"location\": \"myregion\"\r\n }",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Accept-Encoding: gzip, deflate",
"Authorization: ". $fBearer,
"Cache-Control: no-cache",
"Connection: keep-alive",
"Content-Length: 0",
"Content-Type: application/json",
"Host: management.myazurestack.com",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Any idea ?
Thanks
You have to use either Content-Length or chunking, but not both.
This is how you use Content-Length:
Content-Length: 1024
And this is how you use Transfer-Encoding:
Transfer-Encoding: chunked
There is an Azure SDK for PHP which makes things much easier to manage.
i need to get daily invoice numbers from an api endpoint then use these invoice numbers to get additional invoice guest information.
this code hits the API and returns the paid invoice numbers for 3/8/20
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.example.com/api/4.0/invoices?startMin=2020-03-07T00:00:00&endMax=2020-03-08T00:00:00&statusId=PAID",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Accept: /",
"Accept-Encoding: gzip, deflate",
"Content-Type: application/json",
"Authorization: Basic YOUCANTHAVEIT",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Host: api.example.com",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$json = json_decode($response, true);
$json = array_values($json['data']);
$codes = array();
foreach ($json as $BKcodes) {
$codes[] = $BKcodes['code'];
}
curl_close($curl);
}
i need to take each of the invoice numbers and pass to a second endpoint to return additional guest details from the booking invoice. i have changed the above code a little added another $curl2 = curl_init(); with the same connection info changing the CURLOPT_URL to
CURLOPT_URL => "https://api.example.com/api/4.0/bookings/".$BKcodes['code']."/guests",
this works but is painfully slow with only 40 or so invoices.
any direction on a better way would be greatly appreciated
adding
curl_setopt($curl, CURLOPT_URL, "https://api.example.com/api/4.0/bookings/".$BKcodes['code']."/guests");
to the loop sped things up, probably because it did not re authenticate every time.
i am looking into Guzzle thank you #AlexBarker for both, this pointed me in the right direction.
i am also wondering if curl_multi_add_handle() would work in this case.
Google Analytics API access token expires after ~60 Secs.
API URL
https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A199721694&start-date=30daysAgo&end-date=yesterday&metrics=ga%3AuniquePageviews&dimensions=ga%3ApageTitle&sort=-ga%3AuniquePageviews&max-results=4&access_token=ya29.ImGbB1J__QZm322Tt2ig2Q6jPWovnKUGn2IU9nRpFSJF1NxLED9sdmUihLxa-dJCGNp9xXph-L8LECyNnsuqzeKvxNDDoXYmTj2L9aLBUJZFHnxUEbUcJg9c46SdQ6hAnDTz
How to refresh token everytime I need to hit this API using PHP cURL?
I am using this PHP cURL code to hit this API
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A199721694&start-date=30daysAgo&end-date=yesterday&metrics=ga%3AuniquePageviews&dimensions=ga%3ApageTitle&sort=-ga%3AuniquePageviews&max-results=4&access_token=ya29.ImGbB1J__QZm322Tt2ig2Q6jPWovnKUGn2IU9nRpFSJF1NxLED9sdmUihLxa-dJCGNp9xXph-L8LECyNnsuqzeKvxNDDoXYmTj2L9aLBUJZFHnxUEbUcJg9c46SdQ6hAnDTz",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Accept-Encoding: gzip, deflate",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Host: www.googleapis.com",
"Postman-Token: 099c6ed7-97a7-4e00-94ad-599d3775a723,8dde7ea7-9878-44a2-a532-b718a97973de",
"User-Agent: PostmanRuntime/7.17.1",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
I tested an API using postman. Using, postman, I managed to generate a token.
From postman, I managed to get all the data of a user using the following with the following details via a GET request:
https://example.example.co.za/api/consumers/get?id=567675675&email=example#sample.com
Under Authorization type, I selected "Bearer token" and pasted the Token in its respective field.
When I click send, I get a success response with the user data.
In PHP, how can I do the same api call (using the id,email and token) inside a php function?
I tried this:
curl_setopt_array($curl, array(
CURLOPT_URL => "example.example.co.za/api/consumers/get?email=example#sample.com&id=567675675",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Authorization: Bearer jhgukfytjfytdytfjgjyfytfjkugfyfdhtklhkugjf",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Content-Type: application/x-www-form-urlencoded",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
return $response;
But when I view the page, instead of the user data, I see this:
How do I make the API call inside a function to get the same result that I have on postman in PHP?
You need to follow the location
CURLOPT_FOLLOWLOCATION => true
https://www.php.net/manual/de/function.curl-setopt.php
Please find a proper CURL call here.
$url = "https://example.example.co.za/api/consumers/get?id=567675675&email=example#sample.com";
$curl = curl_init();
// OPTIONS:
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'APIKEY: 111111111111111111111',
'Content-Type: application/json',
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// EXECUTE:
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
I used this code before and I could receive the email but it doesn't work now .... why?
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => "https://oauth2.googleapis.com/tokeninfo?id_token=".$id,
));
$resp = curl_exec($curl);
curl_close($curl);
try{
$resp = json_decode($resp,true);
if(empty($resp['error']))
return $resp;
else return null;
}catch (\Exception $e)
{
return null;
}
$id is access_token/
i get name,picture,... but no email in array
The simple answer is from the docs:
These fields are only included when the user has granted the "email" OAuth scopes to the application.
About 2 months ago google shutdown google+ api, if you wish to add your code where you acquire the token maybe we could help you a bit more.
We have two ways to solve this problem:
You should use this https://www.googleapis.com/oauth2/v3/userinfo to receive email and you should have access_token
Most programmers do not know the difference between access_token and id_token, and use access_token instead of id_token by mistake. the access_token is a string shorter than id_token and you should run this code for getting email:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.googleapis.com/oauth2/v3/userinfo",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Authorization: Bearer ya29.GlsJBxmiZkSmgMEUlhrtytyJEEI45uQTR8xmmrsZulz7kEOtyrtyytrycyPxCtg_TZ_hz3x5zI5fRlqgJ9ARCnQIEFZaO5o75ZbwicwFGFf_y7PZ8T",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Host: www.googleapis.com",
"Postman-Token: 55ft6892-d0hc-43b0-af22-5eeced9155b5,58b4bd70-355c-4293-933c-30b1c0982b67",
"User-Agent: PostmanRuntime/7.11.0",
"accept-encoding: gzip, deflate",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {echo "cURL Error #:" . $err;} else {echo $response;}
The mobile programmer receives an email retrieval value and the programmer does not need to call the above web service to get the email.
Finally I got some samples of access_token and id_token
access_token:
ya29.GlsJB84Op0099CWZzGrazU449yFBRX1IwA7kjQvUbISnzaP1tGrqzcH7Pdo-vynDF9GZ7L9El_cvMrX2UBedIcq00Ml4Id5wb1459qpxfB3IzHv7dFwTKyV28E5Y
id_token:
eyJhbGciOiJSUzI1NiIsImtpZCI6IjJjM2ZhYzE2YjczZmM4NDhkNDI2ZDVhMjI1YWM4MmJjMWMwMmFlZmQiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLm67b2dsZS5jb20iLCJhenAiOiI3MjI1NDU1MjAwODItdGNycTRhZzZpZjJwZmVsbXEwN2xoM2xlMm5uamljdHIuYXB67uy5nb29nbGV1c2VyY29udGVudC5jb20iLCJhdWQiOiI3MjI1NDU1MjAwODI456NycTRhZzZpZjJwZmVsbX67N2xoM2xlMm5uamljdHIuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJzdWIiOiIxMDg0MDMxNzUyMzc0NDg5OTI2OTQiLCJlbWFpbCI6Im5pZ2h0LmV4dGVybkBnbWFpbC5jb20iLCJlbWFpbF92ZXJpZmllZ567dHJ1ZSwiYXRfaGFzaCI6IkIyVi1GenM5WjVHRm4wQjNEa0FUMXciLCJpYXQiOjE1NTc4Mjc3MTgsImV4cCI6MTU1NzgzMTgh7H0.S6VNWWcSzOqpNdOBBAY-tDmFJQu0ZTU5RSalYnsC0P4KuW6ts8f61PQT5X2a1DXjgV6vj3iu9T1Apj8RzvYezakruLt7doB9CI9vEr1hpOwCcQ3W_o8MkZWZUtU1i4heXM7k3uW9LQy5fa3mdY4pWX0YEa6x--eAnwvUHuJzrRIfn-boV4Rl1LkkCS7AKKMYpOC-_Gej5P9eu5xc7d-xtFr_Cv8ne5pnQKk-5Un9f3IV5SW9mi9AOJI_MdmKg1n-P495TAk5wrLUME5UOJTHgleh__TmfVZwpvvc-UVKtRObJhANubqqiTA_E5O1-TSdrj9yNNhldyxsL6Xb5MfsJQ`