PHP - How do i put the X-CSRF-TOKEN in the headers? - php

How do I put the $token or x-csrf-token to headers?
The curl post works when I put x-csrf-token in the headers manually.
What should I do to put the $token or x-csrf-token automaticly to headers?
When I make a new curl post that gets token and when I put the token automatic on the headers the response of the curl is 404 bad request
This is my script:
$Thing = $_GET['Thing'];
$username = $_GET['username'];
$password = $_GET['password'];
$login = array(
"cvalue" => $username,
"ctype" => "Username",
"password" => $password,
"captchaToken" => $Thing,
"captchaProvider" => "PROVIDER_ARKOSE_LABS"
);
$curl = curl_init('https://auth.roblox.com/v2/login'); // There are many links you can login from
curl_setopt_array($curl,array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_POSTFIELDS => json_encode($login),
CURLOPT_HEADER => true,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 5,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json; charset=utf-8',
'Accept: application/json, text/plain, */*',
'X-CSRF-TOKEN: '
),
CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
));
$response = curl_exec($curl);
$size = curl_getinfo($curl,CURLINFO_HEADER_SIZE);
curl_close($curl);
#$response1 = explode(" ", $response);
#$cookie = $response1[10];
#$search = ".ROBLOSECURITY=";
#$cookie1 = str_replace($search, '', $cookie);
$headers = substr($response,0,$size);
$array = explode("\n",$headers);
foreach($array as $data){
if(strpos($data,'csrf')){
$info = explode(' ',$data);
break;
}
}
$csrf = $info[1];
$token = $csrf;
print_r($token);
print_r($response);

There isn't a way to automatically attach custom headers to all CURL requests. The best way is to wrap your CURL code inside a function and pass the desired CSRF token as a parameter. Here's a demo:
function curl_call(array $params, string $csrf_token)
{
... curl code here...
'X-CSRF-TOKEN: '. $csrf_token,
... remaining curl code here...
}
Then when you want to make a CURL request, you may call the function like this:
curl_call(['url' => '...', etc], 'csrf token string here');

Related

PHP Curl Response

I am currently working with an API for a little project of mine that will only accept POST requests, however no matter what I seem to do I cannot get a response out of it, I have tried echoing the response, I have tried returning it, I have tried using print_r and nothing works. Can someone tell me what I am doing wrong?
<?php
$applicationPass = $_GET['appPass'];
$email = $_GET['email'];
$password = $_GET['password'];
if ($applicationPass == "abc123"){
//
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_URL => 'http://api.minergate.com/1.0/auth/login/',
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:10.0) Gecko/20100101 Firefox/10.0',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'email' => urlencode($_GET['email']),
'password' => urlencode($_GET['password'])
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
echo $resp;
//
}else{
die();
}
?>
Here is my updated code, I get a little bit of a different response with their new API that took forever to find.
<?php
$applicationPass = $_GET['appPass'];
$email = $_GET['email'];
$password = $_GET['password'];
if ($applicationPass == "abc123"){
//
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_URL => 'https://minergate.com/api/2.2/auth/login',
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:10.0) Gecko/20100101 Firefox/10.0',
CURLOPT_CUSTOMREQUEST => 'POST',
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'email: ' . $email,
'password: ' . $password
)),
CURLOPT_POSTFIELDS => array(
'email' => urlencode($email),
'password' => urlencode($password)
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
echo $resp;
//
}else{
die();
}
?>

How to write curl into php?

I have this below php script. I wants to add call function using curl.
How to i write curl on my below script?
I am new in programming. I am still learning it goodly. Please check my code and try to help me adding curl.
My script has not error. Just i wants to add php function replace to curl function.
I hope so, you understand. Sorry for my not good English. thanks
My code is here:
$url = 'https://web.facebook.com/'.$pageid.'/videos/'.$id.'/';
$context = [
'http' => [
'method' => 'GET',
'header' => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.47 Safari/537.36",
],
];
$context = stream_context_create($context);
$data = file_get_contents($url, false, $context);
function cleanStr($str)
{
return html_entity_decode(strip_tags($str), ENT_QUOTES, 'UTF-8');
}
function hd_finallink($curl_content)
{
$regex = '/hd_src_no_ratelimit:"([^"]+)"/';
if (preg_match($regex, $curl_content, $match)) {
return $match[1];
} else {return;}
}
function sd_finallink($curl_content)
{
$regex = '/sd_src_no_ratelimit:"([^"]+)"/';
if (preg_match($regex, $curl_content, $match1)) {
return $match1[1];
} else {return;}
}
$hdlink = hd_finallink($data);
$sdlink = sd_finallink($data);
if ($sdlink || $hdlink) {
echo 'HD Download ';
echo 'SD Download ';
} else {
echo 'Download not showing - Please Reload This Page';
}
Before we can do anything with a cURL request, we need to first instantiate an instance of cURL - we can do this by calling the function curl_init();
Sample code for GET request:
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://testcURL.com/?item1=value&item2=value2',
CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
Sample code for POST request:
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://testcURL.com',
CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
item1 => 'value',
item2 => 'value2'
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
See this link

getting a bad request response when doing a curl call

I am receiving a bad request from the server when I make a curl request. Not sure where I went wrong, but it looks like that some of the CURLOPT options are incorrect. Do you have any idea what needs to be changed?
This is the code:
$url = "https://order.pizzahut.com/site/ajax/find_nearby";
$ch = curl_init($url);
$useragent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3'; // Setting useragent of a popular browser
$header = array("Accept: application/json, text/plain, */*");
$options = [
CURLOPT_USERAGENT => $useragent,
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEJAR => "cookie.txt",
CURLOPT_REFERER =>"https://www.google.com",
CURLOPT_CUSTOMREQUEST=>"POST",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPHEADER =>$header
];
curl_setopt_array($ch, $options);
loadPage("AK", "ANCHORAGE",$ch);
function loadPage($abbreviation,$city,$ch){
global $conn;
$city = strtolower($city);
$near = "$city,"."$abbreviation";
echo $near;
$postData = [
'occasion' => "C",
'near' => "$near",
'zip' => "$near",
'address' => "",
'dine_in' => false,
'store_limit' =>"10",
'locator' =>true
];
$payload = json_encode($postData);
# Convert the post data array to URL encoded string
// $postDataStr = http_build_query($postData);
echo $payload;
$options[CURLOPT_POSTFIELDS] = $payload;
$html = curl_exec($ch);
echo $html;
}
yes you are right, its post request only issue with header it requires valid Cookie in header. I just copied Cookie from browser and paste in header and its works. So the solution is:
loadPage("AK", "ANCHORAGE");
function loadPage($abbreviation,$city){
$city = strtolower($city);
$near = "$city,"."$abbreviation";
$postData = [
'occasion' => "C",
'near' => "$near",
'zip' => "$near",
'address' => "",
'dine_in' => false,
'store_limit' =>"10",
'locator' =>true
];
$url = "https://order.pizzahut.com/site/ajax/find_nearby";
$ch = curl_init($url);
$useragent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3'; // Setting useragent of a popular browser
$header = array(
"Accept: application/json, text/plain, */*",
"Cookie: failover_checked=1; PHPpoolSSL=!4maklee1+k5+tBLWWPR8rI/3M2NcIXVsgM+5XMIVRK/fJJleEnP4hyVoopcRquvCAs/7CbLq/Vfd0cc=; TS011d6839=01166f2bd0e693828ed3fb37eef086402c5626aca6908460b9722b154cb51f244b1dd882278db1bb1f9ac97a3f2d3617b12e7c7b5c; www-origin=yeti-HR-HARYANA-; www-origin-reset=1; exp_last_visit=1143192473; optimizelyEndUserId=oeu1458555784471r0.9519811202417545; _gat_UA-34361514-1=1; QOSESSID=6ohua5ugd55mjupf0p6k32uia6; _dc_gtm_UA-34361514-1=1; exp_last_activity=1458560242; exp_tracker=a%3A5%3A%7Bi%3A0%3Bs%3A12%3A%22site%2Flocator%22%3Bi%3A1%3Bs%3A4%3A%22home%22%3Bi%3A2%3Bs%3A5%3A%22index%22%3Bi%3A3%3Bs%3A23%3A%22site%2Fajax%2Fclear_session%22%3Bi%3A4%3Bs%3A38%3A%22site%2Fajax%2Fcoupons%2FremoveNationalCoupon%22%3B%7D; TS01bded03=01166f2bd034b629d99e25089af83586e4bf2347f7b1f70b91972e835cfd37e486120c510cfabe2bd9856390632a8276d8250ae05869b06cc1d6a7bc77ee826c8b208bc7361686c0b1cbd41a8530948d8fa923b0b3; TS0118ed69=01166f2bd0c9a8c11fd75a87aad59a90e93a811cbea110792544f73601f99f85ef0de952c0bb1c1706650ace601420df60560355a33fa2ecea9f8f89e1fdd807c0f5717ab8fe2b84c6305f1add271e312787310fa30c484c0174c988835bcea2ced9db5fcf; optimizelySegments=%7B%22209642986%22%3A%22referral%22%2C%22209669694%22%3A%22gc%22%2C%22209692476%22%3A%22false%22%2C%22209740042%22%3A%22none%22%2C%22307825635%22%3A%22true%22%7D; optimizelyBuckets=%7B%7D; _ga=GA1.2.700288496.1458555787; optimizelyPendingLogEvents=%5B%5D"
);
$options = [
CURLOPT_USERAGENT => $useragent,
CURLOPT_COOKIESESSION => true,
// CURLOPT_COOKIEJAR => "cookie.txt",
// CURLOPT_REFERER =>"https://www.google.com",
CURLOPT_POST=>true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPHEADER =>$header
];
// $payload = json_encode($postData);
$options[CURLOPT_POSTFIELDS] = http_build_query($postData);
curl_setopt_array($ch, $options);
$html = curl_exec($ch);
echo $html;
}
Output
{"success":true,"response":[{"address":"118 E 6th Ave","city":"Anchorage","state":"AK","zip":"99501","country":"US","phone":"(907)337-2323","lat":61.216325,"long":-149.882477,"distance":0.81,"StoreNumber":"011168","OnlineOrdering":true,"WingStreet":true,"Buffet":false,"DineIn":false},{"address":"100 E Benson Blvd","city":"Anchorage","state":"AK","zip":"99503","country":"US","phone":"(907)337-2323","lat":61.193863,"long":-149.882482,"distance":1.82,"StoreNumber":"011169","OnlineOrdering":true,"WingStreet":true,"Buffet":true,"DineIn":true},{"address":"2205 E Tudor Rd #39","city":"Anchorage","state":"AK","zip":"99507","country":"US","phone":"(907)743-9828","lat":61.1811,"long":-149.840628,"distance":2.59,"StoreNumber":"014640","OnlineOrdering":true,"WingStreet":true,"Buffet":false,"DineIn":false},{"address":"341 Boniface Pkwy, Unit G","city":"Anchorage","state":"AK","zip":"99504","country":"US","phone":"(907)337-2323","lat":61.220935,"long":-149.777626,"distance":2.7,"StoreNumber":"008615","OnlineOrdering":true,"WingStreet":true,"Buffet":false,"DineIn":false},{"address":"3727 Spenard Highway","city":"Anchorage","state":"AK","zip":"99517","country":"US","phone":"(907)337-2323","lat":61.186142,"long":-149.914382,"distance":2.86,"StoreNumber":"010576","OnlineOrdering":true,"WingStreet":true,"Buffet":false,"DineIn":false},{"address":"1921 W Dimond Blvd Ste 112","city":"Anchorage","state":"AK","zip":"99515","country":"US","phone":"(907)337-2323","lat":61.139149,"long":-149.915007,"distance":5.74,"StoreNumber":"009150","OnlineOrdering":true,"WingStreet":true,"Buffet":false,"DineIn":false},{"address":"12870 Old Seward Hwy","city":"Anchorage","state":"AK","zip":"99515","country":"US","phone":"(907)337-2323","lat":61.103643,"long":-149.858735,"distance":7.87,"StoreNumber":"010684","OnlineOrdering":true,"WingStreet":true,"Buffet":false,"DineIn":false},{"address":"11740 Old Glenn Hwy","city":"Eagle River","state":"AK","zip":"99577","country":"US","phone":"(907)337-2323","lat":61.326675,"long":-149.571538,"distance":12.15,"StoreNumber":"008181","OnlineOrdering":true,"WingStreet":true,"Buffet":true,"DineIn":true},{"address":"851 E Parks Hwy","city":"Wasilla","state":"AK","zip":"99654","country":"US","phone":"(907)373-6600","lat":61.58137,"long":-149.429086,"distance":28.87,"StoreNumber":"010324","OnlineOrdering":true,"WingStreet":true,"Buffet":true,"DineIn":true}],"store_info":{"011168":{"address":"118 E 6th Ave","city":"Anchorage","state":"AK","state_name":"Alaska","zip":99501,"phone":"(907) 337-2323","status":"","carryout_open":false,"carryout_close":false,"carryout_future_order":true,"delivery_open":"10:00AM","delivery_close":"11:30PM","delivery_future_order":true,"closure_reason":"","onlineStatus":"online","firstTimeDiscount":false,"currentlyOpen":false,"deliveryAvailable":false,"carryoutAvailable":false,"acceptFutureOrders":true,"landmark":null,"futuredelivery":true,"futurecarryout":true,"pizzaEvoTestStore":true,"offlineStatusMsg":null,"showDeliveryTime":"Y","promiseTimeDel":35,"promiseTimeLastUpdateDel":{"date":"2016-03-21 01:47:00","timezone_type":3,"timezone":"America\/Chicago"},"promiseTimeDelStale":true,"showCarryoutTime":"Y","promiseTimeCo":15,"promiseTimeLastUpdateCo":{"date":"2016-03-20 22:49:00","timezone_type":3,"timezone":"America\/Chicago"},"promiseTimeCoStale":true,"promiseTime":35},"011169":{"address":"100 E Benson Blvd","city":"Anchorage","state":"AK","state_name":"Alaska","zip":99503,"phone":"(907) 337-2323","status":"","carryout_open":false,"carryout_close":false,"carryout_future_order":true,"delivery_open":"11:30AM","delivery_close":"11:00PM","delivery_future_order":true,"closure_reason":"","onlineStatus":"online","firstTimeDiscount":false,"currentlyOpen":false,"deliveryAvailable":false,"carryoutAvailable":false,"acceptFutureOrders":true,"landmark":null,"futuredelivery":true,"futurecarryout":true,"pizzaEvoTestStore":true,"offlineStatusMsg":null,"showDeliveryTime":"Y","promiseTimeDel":35,"promiseTimeLastUpdateDel":{"date":"2016-03-08 21:12:00","timezone_type":3,"timezone":"America\/Chicago"},"promiseTimeDelStale":true,"showCarryoutTime":"Y","promiseTimeCo":15,"promiseTimeLastUpdateCo":{"date":"2016-03-20 23:09:00","timezone_type":3,"timezone":"America\/Chicago"},"promiseTimeCoStale":true,"promiseTime":35},"014640":{"address":"2205 E Tudor Rd #39","city":"Anchorage","state":"AK","state_name":"Alaska","zip":99507,"phone":"(907) 743-9828","status":"","carryout_open":false,"carryout_close":false,"carryout_future_order":true,"delivery_open":"10:00AM","delivery_close":"12:00AM","delivery_future_order":true,"closure_reason":"","onlineStatus":"online","firstTimeDiscount":false,"currentlyOpen":false,"deliveryAvailable":false,"carryoutAvailable":false,"acceptFutureOrders":true,"landmark":null,"futuredelivery":true,"futurecarryout":true,"pizzaEvoTestStore":true,"offlineStatusMsg":null,"showDeliveryTime":"Y","promiseTimeDel":35,"promiseTimeLastUpdateDel":{"date":"2016-03-21 03:09:00","timezone_type":3,"timezone":"America\/Chicago"},"promiseTimeDelStale":true,"showCarryoutTime":"Y","promiseTimeCo":15,"promiseTimeLastUpdateCo":{"date":"2016-03-21 03:07:00","timezone_type":3,"timezone":"America\/Chicago"},"promiseTimeCoStale":true,"promiseTime":35},"008615":{"address":"341 Boniface Pkwy, Unit G","city":"Anchorage","state":"AK","state_name":"Alaska","zip":99504,"phone":"(907) 337-2323","status":"","carryout_open":false,"carryout_close":false,"carryout_future_order":true,"delivery_open":"10:00AM","delivery_close":"12:00AM","delivery_future_order":true,"closure_reason":"","onlineStatus":"online","firstTimeDiscount":false,"currentlyOpen":false,"deliveryAvailable":false,"carryoutAvailable":false,"acceptFutureOrders":true,"landmark":null,"futuredelivery":true,"futurecarryout":true,"pizzaEvoTestStore":true,"offlineStatusMsg":null,"showDeliveryTime":"Y","promiseTimeDel":35,"promiseTimeLastUpdateDel":{"date":"2016-03-21 02:40:00","timezone_type":3,"timezone":"America\/Chicago"},"promiseTimeDelStale":true,"showCarryoutTime":"Y","promiseTimeCo":15,"promiseTimeLastUpdateCo":{"date":"2016-03-20 23:40:00","timezone_type":3,"timezone":"America\/Chicago"},"promiseTimeCoStale":true,"promiseTime":35},"010576":{"address":"3727 Spenard Highway","city":"Anchorage","state":"AK","state_name":"Alaska","zip":99517,"phone":"(907) 743-9877","status":"","carryout_open":false,"carryout_close":false,"carryout_future_order":true,"delivery_open":"12:00PM","delivery_close":"12:00AM","delivery_future_order":true,"closure_reason":"","onlineStatus":"online","firstTimeDiscount":false,"currentlyOpen":false,"deliveryAvailable":false,"carryoutAvailable":false,"acceptFutureOrders":true,"landmark":null,"futuredelivery":true,"futurecarryout":true,"pizzaEvoTestStore":true,"offlineStatusMsg":null,"showDeliveryTime":"Y","promiseTimeDel":35,"promiseTimeLastUpdateDel":{"date":"2016-03-21 01:47:00","timezone_type":3,"timezone":"America\/Chicago"},"promiseTimeDelStale":true,"showCarryoutTime":"Y","promiseTimeCo":15,"promiseTimeLastUpdateCo":{"date":"2016-03-20 19:57:00","timezone_type":3,"timezone":"America\/Chicago"},"promiseTimeCoStale":true,"promiseTime":35},"009150":{"address":"1921 W Dimond Blvd Ste 112","city":"Anchorage","state":"AK","state_name":"Alaska","zip":99515,"phone":"(907) 337-2323","status":"","carryout_open":false,"carryout_close":false,"carryout_future_order":true,"delivery_open":"10:00AM","delivery_close":"12:00AM","delivery_future_order":true,"closure_reason":"","onlineStatus":"online","firstTimeDiscount":false,"currentlyOpen":false,"deliveryAvailable":false,"carryoutAvailable":false,"acceptFutureOrders":true,"landmark":null,"futuredelivery":true,"futurecarryout":true,"pizzaEvoTestStore":true,"offlineStatusMsg":null,"showDeliveryTime":"Y","promiseTimeDel":35,"promiseTimeLastUpdateDel":{"date":"2016-03-21 01:48:00","timezone_type":3,"timezone":"America\/Chicago"},"promiseTimeDelStale":true,"showCarryoutTime":"Y","promiseTimeCo":15,"promiseTimeLastUpdateCo":{"date":"2016-03-21 01:34:00","timezone_type":3,"timezone":"America\/Chicago"},"promiseTimeCoStale":true,"promiseTime":35},"010684":{"address":"12870 Old Seward Hwy","city":"Anchorage","state":"AK","state_name":"Alaska","zip":99515,"phone":"(907) 337-2323","status":"","carryout_open":false,"carryout_close":false,"carryout_future_order":true,"delivery_open":"12:00PM","delivery_close":"11:30PM","delivery_future_order":true,"closure_reason":"","onlineStatus":"online","firstTimeDiscount":false,"currentlyOpen":false,"deliveryAvailable":false,"carryoutAvailable":false,"acceptFutureOrders":true,"landmark":null,"futuredelivery":true,"futurecarryout":true,"pizzaEvoTestStore":true,"offlineStatusMsg":null,"showDeliveryTime":"Y","promiseTimeDel":35,"promiseTimeLastUpdateDel":{"date":"2016-03-21 01:53:00","timezone_type":3,"timezone":"America\/Chicago"},"promiseTimeDelStale":true,"showCarryoutTime":"Y","promiseTimeCo":15,"promiseTimeLastUpdateCo":{"date":"2016-03-21 01:53:00","timezone_type":3,"timezone":"America\/Chicago"},"promiseTimeCoStale":true,"promiseTime":35},"008181":{"address":"11740 Old Glenn Hwy","city":"Eagle River","state":"AK","state_name":"Alaska","zip":99577,"phone":"(907) 694-1086","status":"","carryout_open":false,"carryout_close":false,"carryout_future_order":true,"delivery_open":"10:00AM","delivery_close":"12:00AM","delivery_future_order":true,"closure_reason":"","onlineStatus":"online","firstTimeDiscount":false,"currentlyOpen":false,"deliveryAvailable":false,"carryoutAvailable":false,"acceptFutureOrders":true,"landmark":null,"futuredelivery":true,"futurecarryout":true,"pizzaEvoTestStore":true,"offlineStatusMsg":null,"showDeliveryTime":"Y","promiseTimeDel":35,"promiseTimeLastUpdateDel":{"date":"2016-03-21 01:31:00","timezone_type":3,"timezone":"America\/Chicago"},"promiseTimeDelStale":true,"showCarryoutTime":"Y","promiseTimeCo":15,"promiseTimeLastUpdateCo":{"date":"2016-03-20 22:31:00","timezone_type":3,"timezone":"America\/Chicago"},"promiseTimeCoStale":true,"promiseTime":35},"010324":{"address":"851 E Parks Hwy","city":"Wasilla","state":"AK","state_name":"Alaska","zip":99654,"phone":"(907) 373-6600","status":"","carryout_open":false,"carryout_close":false,"carryout_future_order":true,"delivery_open":"12:00PM","delivery_close":"11:00PM","delivery_future_order":true,"closure_reason":"","onlineStatus":"online","firstTimeDiscount":false,"currentlyOpen":false,"deliveryAvailable":false,"carryoutAvailable":false,"acceptFutureOrders":true,"landmark":null,"futuredelivery":true,"futurecarryout":true,"pizzaEvoTestStore":true,"offlineStatusMsg":null,"showDeliveryTime":"Y","promiseTimeDel":35,"promiseTimeLastUpdateDel":{"date":"2016-03-21 01:12:00","timezone_type":3,"timezone":"America\/Chicago"},"promiseTimeDelStale":true,"showCarryoutTime":"Y","promiseTimeCo":15,"promiseTimeLastUpdateCo":{"date":"2016-03-21 01:12:00","timezone_type":3,"timezone":"America\/Chicago"},"promiseTimeCoStale":true,"promiseTime":35}},"closed_event":"011168","dimensions":{"exp":"b327ba0a88f0d185c2648ce42a7543c6","usertype":"Guest","storeid":"national","requesttime":"06:39:25"},"ga_tracking":{"ga_label":"Bing Success","errors":null},"user_location":{"city":"Haryana","state":"HR","state_name":null}}
First you are setting curl options with $options without CURLOPT_POSTFILEDS and then you set $options CURLOPT_POSTFIELDS within loadPage function, but those new options are not passed to curl handler by curl_setopt_array();
use curl_setopt_array just after
$options[CURLOPT_POSTFIELDS] = $payload;

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.

Server not responding using Curl POST Request

I am using this code to get the contents of a post request url using php curl
Code looks as below:
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://www1.ptt.gov.tr/tr/interaktif/sonuc-yd.php',
CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'barcode' => 'CP021325078TR',
'security_code' => $capcha2
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
echo "<pre>";
var_dump($resp);
echo "</pre>";
The result doesn’t seem to return anything at all.
What is wrong with this code?
Try this:
$url = 'http://www1.ptt.gov.tr/tr/interaktif/sonuc-yd.php';
$postvals = array(
'barcode' => 'CP021325078TR',
'security_code' => $capcha2
);
$resp = Request($url,$postvals);
echo "<pre>"; var_dump($resp); exit;
...
function Request($url,$params=array()){
$ch = curl_init();
$curlOpts = array(
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true
);
if(!empty($params)){
$curlOpts[CURLOPT_POST] = true;
$curlOpts[CURLOPT_POSTFIELDS] = $params;
}
curl_setopt_array($ch,$curlOpts);
$answer = curl_exec($ch);
if (curl_error($ch)) {
echo curl_error($ch); exit;
}
curl_close($ch);
return $answer;
}
EDIT:
I tested this and got:
Could not resolve host: www1.ptt.gov.tr
So make sure you're calling the right endpoint.
Actually you need to set this variable
$captcha2
To use it here -
'security_code' => $capcha2

Categories