Die Webseite, von der ich versuche, Daten abzurufen, füllt ihren Inhalt mit Ajax-Aufrufen auf eine bestimmte Seite auf. Der entsprechende Aufruf ist wie folgt:php cURL Nachahmung von Ajax-Aufrufen
$.ajax({
type: 'POST',
dataType: "json",
data: Data,
url: '/example-ajax.aspx',
timeout: _defaultTimeOut,
...
Und dies ist die HTTP-Anforderung (mit Ausnahme der Post-Daten):
POST /example-ajax.aspx HTTP/1.1
Host: *website*
Connection: keep-alive
Content-Length: 83
Accept: application/json, text/javascript, */*; q=0.01
Origin: *http://website*
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: *referrer*
Accept-Encoding: gzip, deflate
Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: *very long cokie*
Ich kann auch die relevanten Inhalte mit Postman Tool erhalten mit nur Postwerte Einstellung zusammen mit Referrer, Herkunft und Host-Header.
Was ich mit libcurl mache ist, dass ich zuerst eine Verbindung zur Login-Seite mit Login-Informationen herstelle und den Cookie in cookiejar speichere. Dann sende ich eine Anfrage an die oben erwähnte Ajax-Seite, aber es gibt json-Daten mit {Error: Unauthorized} parameter zurück. Es gibt keine Token oder irgendetwas in lokalen oder Webspeichern, nur das Cookie. Wie kann der Server zwischen einem Browser und einer cURL-Anfrage unterscheiden?
meinen Code Hier ist das Cookie abzurufen:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiejar);
//set the cookie the site has for certain features, this is optional
curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0");
//curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://example.com");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Host: example.com',
'Origin: http://example.com'
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo)
Und hier ist meine Anfrage:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL, $url;
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiejar);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://example.com");
curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Host: example.com',
'Origin: http://example.com',
'X-Requested-With:XMLHttpRequest',
'Accept:application/json, text/javascript, */*; q=0.01',
'Accept-Language:tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4',
'Cache-Control:max-age=0',
'Connection:keep-alive',
'Content-Length:83',
'Content-Type:application/x-www-form-urlencoded; charset=UTF-8',
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
$result = curl_exec($ch);
curl_close($ch);