我正在尝试使用curl登录yahoo。这是我使用的样本类。
<?php
class Yahoo
{
public $cookies = 'cookies.txt';
private $user = null;
private $pass = null;
/*Data generated from cURL*/
public $content = null;
public $response = null;
/* Links */
private $url = array(
'login' => 'http://smallbusiness.yahoo.com/dashboard/mybusinesses?brand=local',
'submit' => 'https://login.yahoo.com/config/login?'
);
/* Fields */
public $data = array();
public function __construct ($user, $pass)
{
$this->user = $user;
$this->pass = $pass;
}
public function login()
{
$this->cURL($this->url['login']);
if($form = $this->getFormFields($this->content, 'fsLogin'))
{
//echo "<pre>".print_r($form,true);exit;
$form['login']= $this->user;
$form['passwd'] = md5(md5($this->pass).$form['.challenge']);
$form['passwd_raw'] = $this->pass;
$form[".md5"] = 1;
$form[".hash"] = 1;
$form[".js"] = 1;
//echo "<pre>".print_r($form,true);exit;
$this->cURL($this->url['submit'], $form);
echo $this->content;exit;
}
echo $this->content;exit;
}
}
/* Scan for form */
private function getFormFields($data, $id)
{
if($id =! 'fsLogin'){
//echo "aw";
if (preg_match('/(<form.*?id=.?'.$id.'.*?<\/form>)/is', $data, $matches)) {
$inputs = $this->getInputs($matches[1]);
return $inputs;
} else {
return false;
}
}else{
$data = preg_replace('/<script.*?<\/script>/is','', $data);
$data = preg_replace('/<style.*?<\/style>/is','', $data);
$data = preg_replace('/<!--.*?-->/is', '', $data);
$data = preg_replace('/<!\[.*?\]>/is','', $data);
$data = preg_replace('/<noscript.*?<\/noscript>/is','', $data);
if (preg_match_all('/<fieldset.*>(.*)?<\/fieldset>/is', $data, $matches)) {
$inputs = $this->getInputs($matches[0][0]);
return $inputs;
} else {
return false;
}
}
}
/* Get Inputs in form */
private function getInputs($form)
{
$inputs = array();
$elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);
if ($elements > 0) {
for($i = 0; $i < $elements; $i++) {
$el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);
if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
$name = $name[1];
$value = '';
if (preg_match('/value=(?:["\'])?([^"\']*)/i', $el, $value)) {
$value = $value[1];
}
$inputs[$name] = $value;
}
}
}
return $inputs;
}
/* Perform curl function to specific URL provided */
public function cURL($url, $post = false)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookies);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookies);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
if($post) //if post is needed
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
}
curl_setopt($ch, CURLOPT_URL, $url);
$this->content = curl_exec($ch);
$this->response = curl_getinfo( $ch );
$this->url['last_url'] = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
}
}
?>
我以某种方式设法以登录形式到达并获取登录表单中的所有字段。但当我试图发布它$ this-&gt; url ['submit']我只看到登录表单而不是带有商家列表列表的页面。任何想法如何使这项工作?