2013-10-06 32 views
11

Curl (PHP aracılığıyla) kullanarak bir siteyi kazıyorum ve bazı bilgiler varsayılan olarak yalnızca ilk birkaç tanesini gösteren ürünlerin listesidir. Geri kalanlar, ürünlerin tam listesini almak için bir düğmeye tıkladıklarında kullanıcıya gönderilir. Bu, bu listeye geri dönmek için ajax çağrısını tetikler.Curl PHP ile ajax çağrısını taklit etme

İşte JS kullandıkları kısaca geçerli:

curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieLocation); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieLocation); 
curl_setopt($ch, CURLOPT_POST, false); 
curl_setopt($ch, CURLOPT_URL, 'https://www.domain.com/Applications/ViewProducts'); 
curl_setopt($ch, CURLOPT_REFERER, 'https://www.domain.com/'); 
$webpage = curl_exec($ch); 
$productsType = trim(find_by_pattren($webpage, '<input id="productsType" name="productsType" type="hidden" value="(.*?)"')); 
$token = trim(find_by_pattren($webpage, '<input name="__RequestVerificationToken" type="hidden" value="(.*?)"')); 

$postVariables = 'productsType='.$productsType. 
'&historyPageIndex=1 
&displayPeriod=0 
&__RequestVerificationToken='.$token; 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postVariables); 
curl_setopt($ch, CURLOPT_URL, 'https://www.domain.com/ajax/getProductList'); 
curl_setopt($ch, CURLOPT_REFERER, 'https://www.domain.com/Applications/ViewProducts'); 
$webpage = curl_exec($ch); 

Bu site ile bir hata sayfası üretir:

headers['__RequestVerificationToken'] = token; 
$.ajax({ 
type: "post", 
url: "/ajax/getProductList", 
dataType: 'html', 
data: JSON.stringify({ historyPageIndex: 1, displayPeriod: 0, productsType: All }), 
contentType: 'application/json; charset=utf-8', 
success: function (result) { 
    $(target).html(""); 
    $(target).html(result); 
}, 
beforeSend: function (XMLHttpRequest) { 
    if (headers['__RequestVerificationToken']) { 
     XMLHttpRequest.setRequestHeader("__RequestVerificationToken", headers['__RequestVerificationToken']); 
    } 
} 
}); 

İşte benim PHP script. Ben ana nedeni bu olabilir düşünüyorum:

  • Onlar bir ajax isteği belirteci başlığında değil sonrası değişkenler olması gerekir

  • (düzeltmek nasıl hiçbir ipucu) var olup olmadığını kontrol

Herhangi bir fikir?

DÜZENLEME:

curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieLocation); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieLocation); 
curl_setopt($ch, CURLOPT_URL, 'https://www.domain.com/Applications/ViewProducts'); 
curl_setopt($ch, CURLOPT_REFERER, 'https://www.domain.com/'); 
$webpage = curl_exec($ch); 
$productsType = trim(find_by_pattren($webpage, '<input id="productsType" name="productsType" type="hidden" value="(.*?)"')); 
$token = trim(find_by_pattren($webpage, '<input name="__RequestVerificationToken" type="hidden" value="(.*?)"')); 

$postVariables = json_encode(array('productsType' => $productsType, 
'historyPageIndex' => 1, 
'displayPeriod' => 0)); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-With: XMLHttpRequest", "Content-Type: application/json; charset=utf-8", "__RequestVerificationToken: $token")); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postVariables); 
curl_setopt($ch, CURLOPT_URL, 'https://www.domain.com/ajax/getProductList'); 
curl_setopt($ch, CURLOPT_REFERER, 'https://www.domain.com/Applications/ViewProducts'); 
$webpage = curl_exec($ch); 

cevap

11

daha yakından bir başlık olarak talep doğrulama kodunu ayarlanmış bir AJAX istek taklit ve JSON'dan kullanımı CURLOPT_HEADER içerik tipi belirlemek için: burada çalışma kodudur.

curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-With: XMLHttpRequest", "Content-Type: application/json; charset=utf-8", "__RequestVerificationToken: $token")); 

Ben de fazlasıyla senin kod satırında 7 false CURLOPT_POST ayarlayarak ve yolluyoruz sonrası verileri JSON biçiminde olmadığını görüyoruz. Sen sahip olmalıdır:

$postVariables = '{"historyPageIndex":1,"displayPeriod":0,"productsType":"All"}'; 
+0

Teşekkür - bu (Öneriniz hala bazı hatalar kaldırdı olarak, bir dizi olarak $ postVariables üzerinde json_encode kullanarak) hafif bir değişiklik ile çalıştı – Davor

+0

@Davor: Bize değişikliği dahil nihai kodunu gösterir misiniz yaptın lütfen? – pablofiumara

+1

@pablofiumara Son çalışma kodunu eklemek için sorumu düzenledim – Davor