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.
Related
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 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.
I have been creating a script to insert data from a JSON file to a POST request.
While it does what it needs to download the image in the URL supplied than it needs to download it and perform a post request on my route.
I have been struggling for the past few hours to get it working but no success.
I have used realpath and CurlFile but it didn't make it work.
Here is the request:
<?php
$image = $this->makeCurlFile(realpath("scorpion_stretch-59788999e4432d80.jpg"));
$body = ["name" => "uploaded_file", "filename" => $image->name];
echo print_r($body);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://resources.diamondappgroup.com/upload.php",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Content-Type: application/x-www-form-urlencoded",
"Host: resources.diamondappgroup.com",
"Postman-Token: 45ebd192-9621-482c-8735-2061bc31c6fa,6a7a959f-372d-4afb-9efa-6b4da47ea1b0",
"User-Agent: PostmanRuntime/7.11.0",
"accept-encoding: gzip, deflate",
"cache-control: no-cache",
"content-length: 312659",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
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`
I created a POST request on POSTMAN, it work great. But when I export header and form body in PHP cURL code and execute it with PHP CLI, the server does not show me data asked in my form.
Thereafter the PHP cURL exported with POSTMAN :
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://renovation-info-service.gouv.fr/trouvez-un-professionnel",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "all%5Bcols-1%5D%5Bcode_postal%5D=03&all%5Bcols-1%5D%5Bcommune%5D=03&all%5Brayon%5D=5&all%5Bdomaine_travaux%5D=30&op=Localiser%2Bdes%2Bentreprises&form_build_id=form-48ViCRy2vxhZBo452cPiRi22VR7K2mZ5ZUwzR-tlO3U&form_id=front_office__locate_all_entreprise_form",
CURLOPT_HTTPHEADER => array(
"accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"accept-encoding: gzip, deflate",
"accept-language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3",
"cache-control: no-cache",
"connection: keep-alive",
"content-length: 253",
"content-type: application/x-www-form-urlencoded",
"cookie: UUID=3ab96f55-1e99-d7c4-39a9-59345773b4ca; has_js=1; _ga=GA1.3.1779497884.1533297982; _gid=GA1.3.1474901001.1533297982; _gat=1",
"host: renovation-info-service.gouv.fr",
"postman-token: 570c2dd8-8289-dacd-db57-dcb0d107ea29",
"referer: http://renovation-info-service.gouv.fr/trouvez-un-professionnel",
"upgrade-insecure-requests: 1",
"user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Thank you for your help ! And sorry with my bad english ^^'
POSTMAN answer is a html code with data asked in POST request. PHP cURL answer is a html code without data asked in POST request.