Writing a CURL request in PHP [closed] - php

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;

Related

Postman exported curl not works but in Postman Works

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"}

How to add google authentication in headers with curl in php

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.

POSTMAN export don't work with PHP cURL

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.

How to post data with cURL?

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

PHP curl extract download link

I'm trying to extract a download link from this website using cURL.
http://abelhas.pt/Asantino/TUTORIAIS/SONY+VEGAS/ZLICED+TRAILER/ZLICED+TRAILER/ZLICED+TRAILER+PRE-RENDERD+FX,36948354.mp4(video)
Opening the link gives u a download button, pressing it will then call a ajax script that returns some J SON data from following URL
http://abelhas.pt/action/License/Download
This URL expects 2 parameters ( fileId(int) & __RequestVerificationToken(str)) as POST parameters and this header needs to be send aswell (X-Requested-With: XMLHttpRequest)
This is the script I'm using to login & fetch the __RequestVerificationToken && the fileId from the Item page
$options = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie:".$cookie.";\r\n" . // check function.stream-context-create on php.net
"User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n"
)
);
$context = stream_context_create($options);
$html = file_get_html($url,false,$context);
$token = $html->find('input[name=__RequestVerificationToken]',0)->value;
$id = $html->find('a.fileCopyAction',0)->rel;
return array(
'id' => $id,
'token' => $token
);
Followed by this code that I use to extract the download link(this does not work)
$res = array();
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // do not return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_POSTFIELDS => 'fileId='.$file_id.'&__RequestVerificationToken='.$token,
CURLOPT_HTTPHEADER => array(
'Accept:*/*',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language: en-US,en;q=0.8',
'Cache-Control: max-age=0',
'Connection: keep-alive',
'Content-Type: application/x-www-form-urlencoded',
'Cookie: '.$cookie,
'Host: abelhas.pt',
'Origin: http://abelhas.pt',
'X-Requested-With: XMLHttpRequest',
),
);
$ch = curl_init("http://abelhas.pt/action/License/Download");
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$res['content'] = $content;
$res['url'] = $header['url'];
return $res;
This returns J SON data but not what the website returns if u inspect it with chrome/firefox.
So the question remains : How to do this to get the same results as if visiting it urself using a browser?
Thanks in advance.

Categories