当我发出curl请求时,我从服务器收到bad request
。不确定我哪里出错,但看起来有些CURLOPT
选项不正确。你知道需要改变什么吗?
这是代码:
$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;
}
答案 0 :(得分:1)
首先使用$ options设置curl选项而不使用CURLOPT_POSTFILEDS,然后在loadPage函数中设置$ options CURLOPT_POSTFIELDS,但这些新选项不会通过curl_setopt_array()传递给curl处理程序;
在
之后使用curl_setopt_array$options[CURLOPT_POSTFIELDS] = $payload;
答案 1 :(得分:1)
是的,你是对的,它的帖子请求只发出标题,它在标题中需要有效的Cookie
。我刚刚从浏览器中复制了Cookie并粘贴了标题及其作品。所以解决方案是:
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;
}
<强>输出强>
{"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}}