2015-10-04 1 views
0

Je dois obtenir le code source d'une page qui n'est accessible que lorsque je suis connecté. Cependant, lorsque j'utilise le code suivant, il ne reconnaît pas que je suis connecté, et me demande de me reconnecter. J'ai fait des recherches et trouvé des exemples similaires à la mienne, mais ils ne m'aident pas. Quand j'utilise le code ci-dessus, il ne reconnaît pas mes cookies.Utiliser cookie avec cURL PHP

code complet:

$USERNAME = 'myEmail'; 
$PASSWORD = 'myPassword'; 
$COOKIEFILE = tempnam ("/tmp", "CURLCOOKIE"); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); 
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $COOKIEFILE); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $COOKIEFILE); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); 
curl_setopt($ch, CURLOPT_TIMEOUT, 120); 

curl_setopt($ch, CURLOPT_URL, 
    'https://accounts.google.com/ServiceLogin?hl=en&service=alerts&continue=http://www.google.com/alerts/manage'); 
$data = curl_exec($ch); 

$formFields = getFormFields($data); 

$formFields['Email'] = $USERNAME; 
$formFields['Passwd'] = $PASSWORD; 
unset($formFields['PersistentCookie']); 

$post_string = ''; 
foreach($formFields as $key => $value) { 
    $post_string .= $key . '=' . urlencode($value) . '&'; 
} 

$post_string = substr($post_string, 0, -1); 

curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth'); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); 

$result = curl_exec($ch); 
$ch = curl_init ("http:/example.com"); 
curl_setopt ($ch, CURLOPT_COOKIEFILE, $COOKIEFILE); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); 
$output = curl_exec ($ch); 
var_dump($output); 
+0

duplicata possible de http://stackoverflow.com/questions/10307744/how-to-login-in-with-curl-and-ssl-and-cookies –

Répondre

0

Allen, dans des situations comme la vôtre, quand je vais avoir connexion de problèmes avec boucle, ce que je ne normalement est d'utiliser Live Http Headers for firefox pour enregistrer mes demandes, puis utilisez les en-têtes de demande avec curlCURLOPT_HTTPHEADER, le cookies sera inclus dans le headers.

Débit:

Connectez-vous au site Web en utilisant votre navigateur, une fois connecté, connectez-vous toute demande avec Live Http Headers for firefox, copiez et collez la demande à un array, quelque chose comme:

$myHeaders = array(
"Host: stackoverflow.com", 
"User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0", 
"Accept: application/json, text/javascript, */*; q=0.01", 
"Accept-Language: en-US,en;q=0.5", 
"Accept-Encoding: gzip, deflate", 
"DNT: 1", 
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8", 
"X-Requested-With: XMLHttpRequest", 
"Referer: http://stackoverflow.com/questions/32933636/use-cookie-with-curl-php", 
"Content-Length: 482", 
"Cookie: mycookie :)", 
"Connection: keep-alive", 
"Pragma: no-cache" 
); 

Ensuite, , utilisez le headers avec curl:

curl_setopt($ch, CURLOPT_HTTPHEADER, $myHeaders); 

Remarques:
1- Pas besoin de CURLOPT_COOKIEJAR ou CURLOPT_COOKIEFILE
2- Cela fonctionne avec presque tous les sites que je l'ai essayé.
3- Certains cookies peuvent expirer après un certain temps, d'autres, comme google, n'expirent jamais.