2013-10-06 16 views
11

Sto raschiando un sito utilizzando curl (tramite PHP) e alcune informazioni che desidero sono un elenco di prodotti che per impostazione predefinita mostra solo i primi. Il resto viene passato all'utente quando fa clic su un pulsante per ottenere l'elenco completo dei prodotti, che attiva una chiamata ajax per restituire quell'elenco.Mimando una chiamata Ajax con Curl PHP

Ecco in poche parole i JS usano:

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']); 
    } 
} 
}); 

Ecco il mio script PHP:

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); 

Questo produce una pagina di errore con il sito. Penso che i motivi principali potrebbe essere che:

  • Controllano se si tratta di una richiesta AJAX (idea di come risolvere questo)

  • Il token deve essere nella testata e non nelle variabili POST

Qualche idea?

EDIT: qui è il codice di lavoro:

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); 

risposta

11

Per impostare il token di richiesta di verifica come intestazione, più strettamente imitano una richiesta AJAX, e impostare il tipo di contenuto a JSON, uso CURLOPT_HEADER.

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

Ho anche notato che si sta impostando superfluo CURLOPT_POST false sulla linea 7 del codice, e che i dati post che stai inviando non è in formato JSON. Si dovrebbe avere:

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

Grazie - ha funzionato con una leggera modifica (utilizzando json_encode su $ postVariables come un array, come il suo suggerimento ancora sollevato alcuni errori) – Davor

+0

@Davor: Ci può mostrare il codice finale compreso il cambiamento hai fatto, per favore? – pablofiumara

+1

@pablofiumara Ho modificato la mia domanda per aggiungere il codice di lavoro finale – Davor