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.
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.
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'm trying to make a simple curl post request using PHP code snippet from Postman. But when I'm trying to run it I get the following error: "OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054"
In postman the request is working fine and I do get the json results.
Here's an inline link for postman header
and the postman body
My php code is the following:
$curl = curl_init();
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.oglasnik.hr/ajax/getNearBy",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 500,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"classifiedId\"\r\n\r\n3996266\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"bounds\"\r\n\r\n{\"east\":16.00793838500977,\"west\":15.977039337158205,\"north\":45.78652940999985,\"south\":45.773958941535355}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"Accept: application/json, text/javascript, */*; q=0.01",
"Accept-Encoding: gzip, deflate, br",
"Accept-Language: en-US,en;q=0.9",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Content-Length: 384",
"Content-Type: application/x-www-form-urlencoded",
"Cookie: cookieconsent_status=allow; ogl_visits^[0^]=a5cbwGRWU7YEC1LP6tKOVOzf6GIZzyU7dbry64BfNak4xygExIAj^%^2FkGWQ5azKqmznEHJVCF4ZfufdAaCugfPjPi8vX7CB8Gs^%^2F3OdX5RVvYjyfWKHUHboChkYfbURk6Ly",
"Host: www.oglasnik.hr",
"Origin: https://www.oglasnik.hr",
"Postman-Token: 60eebbd3-ed26-4aed-ae32-f190353ce333,aa32ad1d-55c6-4fa6-928e-3309a497c36a",
"Referer: https://www.oglasnik.hr/stanovi-najam/novi-zagreb-bundek-2-sobni-stan-55-m2-oglas-3996266",
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
"X-Requested-With: XMLHttpRequest",
"cache-control: no-cache",
"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;
}
UPDATE:
With the tip from #Andrew I changed the CURLOPT_HTTPHEADER to:
CURLOPT_HTTPHEADER => array(
"Accept: application/json, text/javascript, */*; q=0.01",
"Accept-Encoding: gzip, deflate, br",
"Accept-Language: en-US,en;q=0.9",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Content-Type: application/x-www-form-urlencoded",
"Cookie: cookieconsent_status=allow; ogl_visits^[0^]=a5cbwGRWU7YEC1LP6tKOVOzf6GIZzyU7dbry64BfNak4xygExIAj^%^2FkGWQ5azKqmznEHJVCF4ZfufdAaCugfPjPi8vX7CB8Gs^%^2F3OdX5RVvYjyfWKHUHboChkYfbURk6Ly",
"Host: www.oglasnik.hr",
"Origin: https://www.oglasnik.hr",
"Referer: https://www.oglasnik.hr/stanovi-najam/novi-zagreb-bundek-2-sobni-stan-55-m2-oglas-3996266",
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
"X-Requested-With: XMLHttpRequest",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
)
And i'm getting the response! Unfortunately its only
{
"status": false
}
As it looks like I'm sending my post parameters wrong. Problem is that I need to send one integer parameter and one string as json, currently trying like this:
$bounds =
[
"east" => 16.00793838500977,
"west" =>15.977039337158205,
"north" =>45.78652940999985,
"south" =>45.773958941535355
];
$params = [
"classifiedId" => 3996266, "bounds" => json_encode($bounds)
];
and adding this parameter in curl_setopt_array:
CURLOPT_POSTFIELDS => $params
Final update:
It worked! As #Andrew stated in the comments it worked using:
CURLOPT_POSTFIELDS => http_build_query($params)
so the final header is:
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.oglasnik.hr/ajax/getNearBy",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => http_build_query($params),
CURLINFO_HEADER_OUT => true,
CURLOPT_HTTPHEADER => array(
"Accept: application/json, text/javascript, */*; q=0.01",
"Accept-Encoding: gzip, deflate, br",
"Accept-Language: en-US,en;q=0.9",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Content-Type: application/x-www-form-urlencoded",
"Cookie: cookieconsent_status=allow; ogl_visits^[0^]=a5cbwGRWU7YEC1LP6tKOVOzf6GIZzyU7dbry64BfNak4xygExIAj^%^2FkGWQ5azKqmznEHJVCF4ZfufdAaCugfPjPi8vX7CB8Gs^%^2F3OdX5RVvYjyfWKHUHboChkYfbURk6Ly",
"Host: www.oglasnik.hr",
"Origin: https://www.oglasnik.hr",
"Referer: https://www.oglasnik.hr/stanovi-najam/novi-zagreb-bundek-2-sobni-stan-55-m2-oglas-3996266",
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
"X-Requested-With: XMLHttpRequest",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
),
));
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 have created php curl to send xml request as under, where it seems that login works. because if there is anything wrong in login detail then it shows error. but if everything is ok then it give response in some garbage value:
Source main code :
$contentlength=strlen($postdata);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL =>$this->url ,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "$postdata",
CURLOPT_HTTPHEADER => array(
"Content-Type: text/xml;charset=utf-8",
"Content-Length: 839",
"Accept: text/xml",
"Accept-Encoding: gzip",
"Authorization : $Authorization",
"User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11",
"Host: $host",
"Connection: close"
),
));
Ouput looks like this :
‹d1oÂ0…÷üŠS–,Û)J›ÈuèÔ"$–²šäH£Æv°MUþ}MÒ€J·»{ŸŸï_}«¾ÐºÖèeÂæ4Ô•©[Ý,““?¤OÉJDÜ¢ëv¸ïLõyåã‡yþ‹ Ç:oñ€6 x
what is wrong ? should i decode anything ? if yes then in which forgat i need to debug. I use securetrading.com site to send xml request.
Simply, you have to remove "Accept-Encoding: gzip" from CURLOPT_HTTPHEADER array.
If still not working, use to decompress output using gzdecode() function
$original_output= gzdecode($curl_output);
For resolve issue of content length you have to count string length of post data, which you have already calculated and stored in $contentlength variable.
"Content-Length:".$contentlength
curl_setopt($curl, CURLOPT_ENCODING , "gzip");
will show properly the result