Skip to content

Instantly share code, notes, and snippets.

@slywalker
Last active December 11, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slywalker/4670281 to your computer and use it in GitHub Desktop.
Save slywalker/4670281 to your computer and use it in GitHub Desktop.
<?php
class YAHOO_CONFIG {
public $default = array(
'login' => 'your_yahoo_account',
'passwd' => 'your_yahoo_password',
);
}
<?php
/**
* require QueryPath
*
**/
App::uses('AppModel', 'Model');
App::uses('HttpSocket', 'Network/Http');
App::import('Vendor', 'autoload');
class YahooAuction extends AppModel {
public $useTable = false;
public $useYahooConfig = 'default';
public $YahooConfig;
public $HttpSocket;
public $loginUrl = 'https://login.yahoo.co.jp/config/login';
public $loginQuery = array(
'.lg' => 'jp',
'.intl' => 'jp',
'.src' => 'auc',
'.done' => 'http://auctions.yahoo.co.jp/'
);
public $userAgent = 'Mozilla/6.0 (Windows; U; Windows NT 6.0; ja; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1 (.NET CLR 3.5.30729)';
public $cookies = array();
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
include_once APP . 'Config' . DS . 'yahoo.php';
if (class_exists('YAHOO_CONFIG')) {
$this->YahooConfig = new YAHOO_CONFIG();
} else {
throw new Exception('YAHOO_CONFIG is not exists.');
}
$this->HttpSocket = new HttpSocket(array(
'request' => array(
'redirect' => true,
'header' => array(
'User-Agent' => $this->userAgent
)
)
));
}
public function httpGet($url, $query = array(), $options = array()) {
$options += array(
'cookies' => $this->cookies
);
if (isset($options['referer'])) {
$options += array(
'header' => array(
'Referer' => $options['referer']
)
);
unset($options['referer']);
}
$Response = $this->HttpSocket->get($url, $query, $options);
$this->HttpSocket->reset();
$this->cookies($Response->cookies);
return $Response;
}
public function httpPost($url, $data = array(), $options = array()) {
$options += array(
'cookies' => $this->cookies
);
if (isset($options['referer'])) {
$options += array(
'header' => array(
'Referer' => $options['referer']
)
);
unset($options['referer']);
}
$Response = $this->HttpSocket->post($url, $data, $options);
$this->HttpSocket->reset();
$this->cookies($Response->cookies);
return $Response;
}
public function login() {
$this->httpGet('http://auctions.yahoo.co.jp/');
$Response = $this->httpGet($this->loginUrl, $this->loginQuery, array(
'referer' => 'http://auctions.yahoo.co.jp/'
));
$Html = QueryPath::withHTML($Response->body(), null, array(
'convert_to_encoding' => 'UTF-8'
));
$albatross = null;
if (preg_match_all(
'/document\.getElementsByName\("\.albatross"\)\[0\]\.value = "(.*?)";/',
$Html->find('script')->text(),
$matches
)) {
$albatross = $matches[1][0];
}
$data = $this->YahooConfig->{$this->useYahooConfig} + array(
'.persistent' => 'y',
'.albatross' => $albatross
);
$Form = $Html->find('#login_form');
$Form->remove('noscript');
$hiddenFields = $Form->find('input');
foreach ($hiddenFields as $field) {
if ($field->attr('type') === 'hidden' && !isset($data[$field->attr('name')])) {
$data[$field->attr('name')] = $field->attr('value');
}
}
sleep(3);
$Response = $this->httpPost($Form->attr('action'), $data, array(
'referer' => $Form->attr('action')
));
if (!$Response->cookies) {
throw new Exception('Yahoo could not login.');
}
}
public function cookies($cookies = array()) {
if (!empty($cookies)) {
$this->cookies = array_merge($this->cookies, $cookies);
} else {
return $this->cookies;
}
}
}
<?php
App::uses('YahooAuction', 'Model');
class YahooAuctionTest extends CakeTestCase {
public $fixtures = array();
public function setUp() {
parent::setUp();
$this->YahooAuction = ClassRegistry::init('YahooAuction');
}
public function tearDown() {
unset($this->YahooAuction);
parent::tearDown();
}
public function testLogin() {
$this->YahooAuction->login();
$Response = $this->YahooAuction->httpGet('http://auctions.yahoo.co.jp/');
$Html = QueryPath::withHTML($Response->body(), null, array(
'convert_to_encoding' => 'UTF-8'
));
var_dump($Html->find('.decTd01 b')->text());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment