i have a problem whit postman i am develope a connectio to a api and i am experience a strange unsolved error.
I am try to post a excel file to a server , in Postman all woprks fine and im able to upload the file whit no problem. But if i export the code generated , they return a " Invalid File" error.
i have try all and no way to find what is going wrong here
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://eiffel-monorepo-stock.aws.paris.cl/stock/import/7707ecb8-ebc8-4f37-86f6-4a658a46becc',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('file'=> new CURLFILE('/home/alakazan/Descargas/stock_1666535230.xlsx')),
CURLOPT_HTTPHEADER => array(
'Host: [[[EXCLUDED DATA]]]',
'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:106.0) Gecko/20100101 Firefox/106.0',
'Accept: application/json, text/plain, */*',
'Accept-Language: es,es-CL;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding: gzip, deflate, br',
'Referer: [[[EXCLUDED DATA]]]',
'Authorization: Bearer [[[EXCLUDED DATA]]],
'Content-Type: multipart/form-data; boundary=---------------------------83496323188075087905603769295',
'Content-Length: 15844',
'Origin: [[[EXCLUDED DATA]]]',
'Connection: keep-alive',
'Sec-Fetch-Dest: empty',
'Sec-Fetch-Mode: no-cors',
'Sec-Fetch-Site: same-site',
'Pragma: no-cache',
'Cache-Control: no-cache'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
I try to upload using console and error are Invalid file :(
{"statusCode":400,"error":true,"message":"Invalid File"}
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 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.
I want to return the response as url instead boolean. I looks header from browser and match it but it only return boolean (1). Is there some options missing?
<?php
$ch = curl_init();
$url = 'http://www.tusfiles.net/anfossy50rrd';
// Post field and value
$post = array(
'op' => 'download2',
'id' => 'anfossy50rrd',
'rand' => 'q56tfpiklusrizipkforyjqsykxzqlsi7ur3hyi',
'referer' => '',
'method_free' => '',
'method_premium' => '',
'down_script' => '1'
);
its the header
$headers = [
'Host: www.tusfiles.net',
'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: en-US,en;q=0.5',
'Accept-Encoding: gzip, deflate',
'Cache-Control: no-cache',
'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
'Referer: http://www.tusfiles.net/anfossy50rrd',
'Cookie: lang=english; login=lynxpravoka; xfss=; aff=2513158; __atuvc=17%7C36;
__atuvs=57cf088127fd55f9004; cookiescriptaccept=visit',
'Connection: keep-alive',
'Upgrade-Insecure-Requests: 1'
];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
$server_output = curl_exec ($ch);
print_r($server_output);
?>
You need to use something like this:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Header-1: value1',
'X-Header-2: value2'
));
Or you can also build an array where the key is the header name and the value is of course the header value. It would look something like this:
$headers = array(
'Host' => 'www.tusfiles.net',
'User-Agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
http://www.php.net/manual/en/function.curl-setopt.php
the problem is that the server returns a HTTP/1.1 302 Found HTTP redirect, expecting you to send the request to the "found" URL. but because you do not have CURLOPT_FOLLOWLOCATION - nor do you manually handle redirects (nor read the redirect headers at all), you do not follow the redirect. enable CURLOPT_FOLLOWLOCATION , and you should get the response.
also, you should practically never give the 'Accept-Encoding: gzip, deflate', header manually with curl. if curl was not actually compiled with gzip or deflate and the server decides to use it, the transfer will fail, furthermore, if, in the future, both the server and curl supports an even better transfer encoding, it won't be used because you explicitly said only gzip/deflate was supported. instead, make curl give all supported encodings automatically, by giving an empty string '' to CURLOPT_ENCODING.
and a protip: in the future, to debug why curl transfers isn't working as expected, enable CURLOPT_VERBOSE
What am I doing wrong? This is my code:
$json_url = ";
$apikey = "..........";
$data_json = '{"user":"...........",
"customer" {"username":".....","password":".....","phishing":"....."},"seller":{"username":".....","password":"......","phishing":"....."},"amount":10000,"platform":"ps"}';
$ch = curl_init($json_url);
curl_setopt_array($ch, array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data_json,
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => array(
'Connection: keep-alive',
'Accept: application/json',
'Content-Type: application/json; charset=utf-8',
'Host: futservices.com',
'Content-Length: '.strlen($data_json).'',
'Accept-Encoding: gzip'
))
);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
$order = json_decode($response, true);
echo "Your Token Is: ".$order['d'];`
The request goes through fine, however, I want to grab the "d" JSON element that sent by in a JSON Response from the API.
This is the response:
HTTP/1.1 200 OK Cache-Control: private, max-age=0 Content-Length: 44 Content-Type: application/json; charset=utf-8 Server: Microsoft-IIS/8.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Sat, 25 Apr 2015 16:58:09 GMT {"d":"d922bad4-60d8-4fe4-9cba-0231328744a0"}1Your Token Is:
As you can see "d" is not echoed after "Your Token Is: ".
How can I resolve this?
You need to set the CURLOPT_RETURN_TRANSFER flag to store the result of a CURL as a variable. What's happening is that the response is just being sent straight to your browser. Add the option as I have done below.
curl_setopt_array($ch, array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data_json,
CURLOPT_HEADER => true,
CURLOPT_RETURN_TRANSFER => true,
CURLOPT_HTTPHEADER => array(
'Connection: keep-alive',
'Accept: application/json',
'Content-Type: application/json; charset=utf-8',
'Host: futservices.com',
'Content-Length: '.strlen($data_json).'',
'Accept-Encoding: gzip'
))
);
I hope that helps!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Could someone please help me convert this curl command line request to the equivalent in PHP? Thank you SO much in advance!
curl 'https://utas.fut.ea.com/ut/game/fifa14/trade?tradeIds=3266885522' -H 'Cookie: CEM-session=2s6atve4hl9rdqlbleq8bhvgq5; s_sivo=GB%3AEACOM%3ANONE; s_ria=flash%2011%7Csilverlight%205.1; utag_main=_st:1380330504726$ses_id:1380328780078%3Bexp-session; __utma=103303007.320543511.1380328721.1380328721.1380328721.1; __utmc=103303007; __utmz=103303007.1380328721.1.1.utmcsr=easports.com|utmccn=(referral)|utmcmd=referral|utmcct=/uk/fifa/football-club; s_ppv=100; s_cc=true; s_nr1=1380328918707-NEW; s_sq=%5B%5BB%5D%5D' -H 'Origin: http://www.easports.com' -H 'Accept-Encoding: gzip,deflate,sdch' -H 'Host: utas.fut.ea.com' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'X-UT-SID: 7b8c91aa-083a-4c57-9176-21b53228bcbd' -H 'X-UT-PHISHING-TOKEN: 7869050555705646289' -H 'Referer: http://www.easports.com/iframe/fut/bundles/futweb/web/flash/FifaUltimateTeam.swf' -H 'X-UT-Embed-Error: true' -H 'Connection: keep-alive' -H 'X-HTTP-Method-Override: GET' --data-binary ' ' --compressed
The code of my attempt: http://pastebin.com/86VMFkK7
<?php
$url = 'https://utas.fut.ea.com/ut/game/fifa14/trade?tradeIds=3266885522';
$headers = array(
's_sivo' => 'GB%3AEACOM%3ANONE',
's_ria' => 'flash%2011%7Csilverlight%205.1',
// .. add another headers
'Referer' => 'http://www.easports.com/iframe/fut/bundles/futweb/web/flash/FifaUltimateTeam.swf'
);
$options = array(
CURLOPT_HTTPHEADER => $headers, // Your custom headers
CURLOPT_COOKIEFILE => 'cookiefile.txt', // cURL or you can handled cookie content
CURLOPT_COOKIEJAR => 'cookiefile.txt', // Same as before
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "my-custom-agent", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 10, // timeout on connect
CURLOPT_TIMEOUT => 10, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false // ***Disabled SSL Cert checks
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
echo $content;