How to post data with cURL? - php

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

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

Would a misconfiguration in cookies and/or postfields lead to this 500 internal server error when using wp_remote_post to post data within Wordpress?

I am connecting to a server that does not have a published API and have successfully been able to use the PHP curl functions to retrieve a token and login, but when I try to use Wordpress built-in functions to achieve the same thing, I am not successful and receive a 500 Internal Server Error from the remote host. I believe that I may need to properly configure the cookies and/or data parameters.
I am able to retrieve a token from the remote host using wp_remote_get. I have tried changing the Content-Type to text, json and other common alternatives, although 'application/x-www-form-urlencoded' worked fine when using curl. I have tried sending the token in the header as well as in post fields.
// I am trying to get this to work:
$token = get_transient('access_token');
if (!$token)
return;
$args = array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'compress' => true,
'headers' => array(
'Authority' => 'remote.site.com',
'Content-Type' => 'application/x-www-form-urlencoded',
'Referer' => 'remote.site.com/login',
'Cache-Control' => 'max-age=0',
'Upgrade-Insecure-Requests' => 1,
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
'Accept-Encoding' => 'gzip, deflate, br',
'Accept-Language' => 'zh-CN,zh;q=0.9,en;q=0.8,ms;q=0.7,zh-TW;q=0.6,fr;q=0.5',
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3'
),
'cookies' => array(),
'body' => array(
'__RequestVerificationToken' => $token,
'UserName' => USERNAME,
'Password' => PASSWORD,
'RememberMe' => true
)
);
$response = wp_remote_post( 'remote.site.com', $args);
// and no issue doing it this way...
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'remote.site.com/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "__RequestVerificationToken=$token&UserName=JohnDoe&Password=password&RememberMe=true");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'Authority: remote.site.com';
$headers[] = 'Cache-Control: max-age=0';
$headers[] = 'Origin: https://remote.site.com';
$headers[] = 'Upgrade-Insecure-Requests: 1';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8';
$headers[] = 'Referer: https://remote.site.com/login';
$headers[] = 'Accept-Encoding: gzip, deflate, br';
$headers[] = 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,ms;q=0.7,zh-TW;q=0.6,fr;q=0.5';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_HEADER, 1);
When using curl functions, I get 302 response for the login request and subsequent requests are fulfilled, whereas with wp_remote_post I get a response with the login page and a 500 internal server error response.
add code inside the wp-config file
define('COOKIE_DOMAIN', $_SERVER['HTTP_HOST'] );
define( 'ADMIN_COOKIE_PATH', '/' );
define( 'COOKIEPATH', '/' );
define( 'SITECOOKIEPATH', '/' );

How do i convert this cURL command into PHP code? And what is "Request Payload"?

So, I was inspecting the network while I'm uploading an JPG image.
A copy as a curl command :
curl "https://admin-official.line.me/11702069/home/api/objects" -H "Origin: https://admin-official.line.me" -H "Accept-Encoding: gzip, deflate, br" -H "Accept-Language: en-US,en;q=0.9,id;q=0.8,de-DE;q=0.7,de;q=0.6" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36" -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryBv1B9k1i89zlgjnA" -H "Accept: */*" -H "Referer: https://admin-official.line.me/11702069/home/send/" -H "Cookie: tzoffset=-420; _trmccid=6b030cba93d9db09; _ga=GA1.2.1058855825.1526457491; _gid=GA1.2.814129237.1526457491; ldsuid=CiDum1r75zWBJ3f7oLqRAg==; _trmcdisabled2=-1; _trmcuser=^{^\^"id^\^":^\^"^\^"^}; _ga=GA1.3.1058855825.1526457491; _gid=GA1.3.814129237.1526457491; plack_session_localaccount=XXX; minesota=XXX; minesota2=XXX-jdT5f_x10ov7Re12NliQw_zFyIe9osY4oOOc2Rxdy1gMyBhXbKdPmnpA2LHB9VhvlqlL_4hkeSNQK5SgU; plack_session=6cd2e4790ae9990303ea54f7f35b305fba801dc0; _trmcsession=^{^\^"id^\^":^\^"b8ba0724^\^",^\^"path^\^":^\^"/11702069/home/send/^\^",^\^"query^\^":^\^"^\^",^\^"time^\^":1526735963213,^\^"_dice^\^":0.09452730791211028^}; __try__=1526737592637; _trmcpage=/11702069/home/send/" -H "Connection: keep-alive" --data-binary ^"------WebKitFormBoundaryBv1B9k1i89zlgjnA^
Content-Disposition: form-data; name=^\^"file^\^"; filename=^\^"alpen.jpg^\^"^
Content-Type: image/jpeg^
^
^
------WebKitFormBoundaryBv1B9k1i89zlgjnA^
Content-Disposition: form-data; name=^\^"csrf_token^\^"^
^
vJiSf8AZPpAvqCp0JQtrNOMgrZRtrDpa1TEHbYLz^
------WebKitFormBoundaryBv1B9k1i89zlgjnA--^
^" --compressed
I've never seen this before, i'm not an expert at curl. But I'm using curl to PHP coverter, but it's not working in this case.
EDIT : here's what I've tried.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://admin-official.line.me/11702069/home/api/objects");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("file" => "#".realpath('test.jpg'), "csrf_token" => $split2[0]));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = "Origin: https://admin-official.line.me";
$headers[] = "Accept-Encoding: gzip, deflate, br";
$headers[] = "Accept-Language: en-US,en;q=0.9,id;q=0.8,de-DE;q=0.7,de;q=0.6";
$headers[] = "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36";
$headers[] = "Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryBv1B9k1i89zlgjnA";
$headers[] = "Accept: */*";
$headers[] = "Referer: https://admin-official.line.me/11702069/home/send/";
$headers[] = 'Cookie: tzoffset=-420; _trmccid=6b030cba93d9db09; _ga=GA1.2.1058855825.1526457491; _gid=GA1.2.814129237.1526457491; ldsuid=CiDum1r75zWBJ3f7oLqRAg==; _trmcdisabled2=-1; _trmcuser={"id":""}; _ga=GA1.3.1058855825.1526457491; _gid=GA1.3.814129237.1526457491; plack_session_localaccount=XXX; minesota=XXX; minesota2=XXX; plack_session=XXX; _trmcsession={"id":"b8ba0724","path":"/11702069/home/send/","query":"","time":1526735963213,"_dice":0.09452730791211028}; __try__=1526737592637; _trmcpage=/11702069/home/send/';
$headers[] = "Connection: keep-alive";
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
echo $result;
?>
the "request payload" is the request body. a HTTP request is split into 2 parts, the headers, and the body (and Chrome calls the body for the request payload here, not sure if that's an official name, or just the whim of a chromium dev)
anyway, you're using the # method, which was discouraged as of PHP 5.5, disabled-by-default in php 5.6, and completely removed in PHP7.
use CURLFile instead, eg:
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"file" => new CURLFile('test.jpg'),
"csrf_token" => $split2[0]
));
also, don't set the Accept-Encoding header manually. you specify gzip, deflate, br, and curl doesn't support br, and depending on how libcurl was compiled, might not even support gzip/deflate (although it usually does), remove that.
also, set CURLOPT_ENCODING to emptystring, and curl will list all encodings it was built to support (which is usually gzip and deflate, but is not guaranteed to be)
also, when using the # or CURLFile method, don't set the Content-Type header manually, because if curl's boundary doesn't match what you set in the Content-Type header, the server won't be able to read the request properly. curl will set this header for you, if you don't override it, so don't.
Figure it out using Postman.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://admin-official.line.me/11702069/home/api/objects",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array(
"csrf_token" => $split2[0],
"file" => new CURLFile("test.jpg")
),
//CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"csrf_token\"\r\n\r\nETukCyg_-OM72cZ0MuAyunvvy4cb8Cx2QqoP41T7\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Accept-Encoding: gzip, deflate, br",
"Accept-Language: en-US,en;q=0.9,id;q=0.8,de-DE;q=0.7,de;q=0.6",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Content-Type: multipart/form-data",
"Cookie: tzoffset=-420; _trmccid=6b030cba93d9db09; _ga=GA1.2.1058855825.1526457491; _gid=GA1.2.814129237.1526457491; ldsuid=CiDum1r75zWBJ3f7oLqRAg==; _trmcdisabled2=-1; _trmcuser=^{^\^id^^:^\^^^^}; _ga=GA1.3.1058855825.1526457491; _gid=GA1.3.814129237.1526457491; plack_session_localaccount=COOKIE; minesota=COOKIE; minesota2=COOKIE; _trmcsession=^{^\^id^^:^\^36b6efe6^^,^\^path^^:^\^/11702069/home/send/^^,^\^query^^:^\^^^,^\^time^^:1526880852499,^\^_dice^^:0.4099096438552394^}; plack_session=519cce226206bd15cba86f2153c9dc91a56df4fb; _trmcpage=/11702069/home/send/; __try__=1526881572577",
"Origin: https://admin-official.line.me",
"Postman-Token: 8b9dc9c8-166a-4c50-93c4-b16ca72d72dc",
"Referer: https://admin-official.line.me/11702069/home/send/",
"User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36",
//"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
),
));
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

Empty result with PHP Curl, But response is exists with json format

I want to extract data from this URL with Php Curl
https://www.tiket.com/ajax/pingFlightSearch?d=CGK&a=DPS&date=2017-08-18&adult=1&child=0&infant=0&airlines=%5B%22LION%22%5D&subsidy=true&page_view=roundseperate
But i got an empty page.
I check to that link and inspect with mozilla network->xhr and there is an content in response tab.
How I can extract the data from that link with php curl?
Here is my code
$url = 'https://www.tiket.com/ajax/pingFlightSearch?d=CGK&a=DPS&date=2017-08-18&adult=1&child=0&infant=0&airlines=%5B%22LION%22%5D&subsidy=true&page_view=roundseperate';
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17');
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($cURL, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($cURL, CURLOPT_AUTOREFERER, true);
curl_setopt($cURL, CURLINFO_HEADER_OUT, true);
curl_setopt($cURL, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($cURL, CURLOPT_VERBOSE,true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
"Host" => "www.tiket.com",
"User-Agent" => "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0",
"Accept" => "application/json, text/javascript, */*; q=0.01",
"Accept-Language" => "en-us,en;q=0.5",
"Accept-Encoding" => "gzip, deflate, br",
"Content-Type" => "application/cap+xml;charset=utf-8",
"X-NewRelic-ID" => "UQIGUlJXGwACUFZaAAM=",
"X-Requested-With" => "XMLHttpRequest",
"Connection" => "keep-alive",
"Pragma" => "no-cache",
"Cache-Control" => "no-cache"
));
$result = curl_exec($cURL);
print_r($result);
curl_close($cURL);
I must add 'X-Requested-With: XMLHttpRequest' at header of that link then response will come out.
edit header
Then the response will come out
[response result][2]
You've done a mistake with CURLOPT_HTTPHEADER. You set params as Key => Value but it's incorrect. You should set params as values with ":" as the delimiter.
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
"Host: www.tiket.com",
"Accept: application/json, text/javascript, */*; q=0.01",
"Accept-Language: en-us,en;q=0.5",
"Content-Type: application/cap+xml;charset=utf-8",
"X-NewRelic-ID: UQIGUlJXGwACUFZaAAM=",
"X-Requested-With: XMLHttpRequest",
"Connection: keep-alive",
"Pragma: no-cache",
"Cache-Control: no-cache"
));
Also I have removed this string to get uncompressed data
"Accept-Encoding: gzip, deflate, br",
Mate are u sure that this link work? because I inspected the code with mozilla and chrome and the response is empty, and I tried to scrape with a simple php class that i have with curl and the result is empty cuz the link dont have response

php curl shows non-readable signs

My Curl-code works:
$ch = curl_init();
$header = array(
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Accept-Language: en-us;q=0.8,en;q=0.6'
);
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13',
CURLOPT_HTTPHEADER => $header
);
curl_setopt_array($ch, $options);
$str = curl_exec($ch);
curl_close($ch);
But from one page only I get signs start so:
‹�������í}ëvâH²îïîµö;dk¦}™² ...
I detect with mb_detect_encoding that it is an utf8-string. I'm confused, why can't I read the source code like from other sites?
The site I want to curl is http://stores.ebay.com/artlines-2012
Thank you.
It looks like an encoding issue. Try adding CURLOPT_ENCODING.
Example:
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');

Categories