MISE À JOUR: Il s'avère que mon code fonctionne. Le navigateur était en train de mettre en cache la réponse précédente échouée. Merci pour les pointeurs.Problème d'utilisation de Curl pour se connecter à NYTimes.com
Je construis un prototype et une chose que je voudrais faire est d'effectuer un service si l'utilisateur est un membre valide de NYTimes.com en fournissant leurs informations d'identification. En utilisant curl, j'essaie d'effectuer une connexion au site, puis de vérifier la réussite ou l'échec.
Mon code, ci-dessous, ne renvoie pas d'erreurs, mais je retombe sur la page de connexion:
<?php
class Login {
function Verify() {
print $this->getContent();
}
function getContent() {
$url = 'http://www.nytimes.com/auth/login';
// URI can be any NYT web page to be redirected to upon successful login
// SAVEOPTION and Submit2 are Optional but in original web form so included here
$fields = array(
'is_continue'=> 'true',
'USERID' => urlencode('ENTER_YOUR_USERNAME'),
'PASSWORD' => urlencode('ENTER_A_PASSWORD'),
'URI' => urlencode('http://www.nytimes.com/robots.txt'),
'OQ' => '',
'OP' => '',
'SAVEOPTION' => 'YES',
'Submit2' => 'Log In'
);
$fields_string = '';
if(!$curld = curl_init($url)) {
echo "Could not connect to the specified resource";
exit;
}
$ch = curl_init();
$useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, "curl_login_cookie.txt");
curl_setopt($ch ,CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
ob_start();
curl_exec ($ch);
curl_close ($ch);
$result = ob_get_contents();
ob_end_clean();
return $result;
}
}
$login = new Login;
$result = $login->Verify();
?>
Tous les pointeurs ou suggestions sont les bienvenus.
Pourquoi un utilisateur souhaite-t-il vous donner ses informations d'identification nytimes.com? –
Est-ce que la courbure est affichée? Je ne suis pas un maître PHP ou Curl, mais je pensais que c'était seulement. – tster
@tster curl peut certainement POST. –