Skip to content

Instantly share code, notes, and snippets.

@wjfz
Created July 28, 2017 11:11
Show Gist options
  • Save wjfz/19b11e2ec34354746ef49bd4f4cc6593 to your computer and use it in GitHub Desktop.
Save wjfz/19b11e2ec34354746ef49bd4f4cc6593 to your computer and use it in GitHub Desktop.
jingdong
public function actionJd()
{
$first = JdSku::model()->find("fetched = 0");
echo $first->sku."\n";
$this->doFetch($first->sku);
$first->fetched = 1;
$first->save();
echo "ok\n";
return "ok\n";
}
private function doFetch($sku)
{
$header[] = ":authority:item.jd.com";
$header[] = ":method:GET";
$header[] = ":path:/{$sku}.html";
$header[] = ":scheme:https";
$header[] = "accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
$header[] = "accept-language:zh-CN,zh;q=0.8,zh-TW;q=0.6,en;q=0.4";
$header[] = "dnt:1";
$header[] = "upgrade-insecure-requests:1";
$ch = curl_init();
$options = array(
CURLOPT_URL => "https://item.jd.com/{$sku}.html",
CURLOPT_TIMEOUT => 5,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_AUTOREFERER => false,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HEADER => false,
CURLOPT_ENCODING => 'gzip, deflate, br',
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36',
CURLOPT_HTTPHEADER => $header
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
curl_close($ch);
$result = iconv("GBK", "UTF-8", $result);
// var_dump($result);
preg_match_all("|//item.jd.com/(.*).html|U", $result, $matches);
// var_dump($matches);
$newSkus = array_unique($matches[1]);
foreach ($newSkus as $newSku) {
$newSku = intval($newSku);
$check = Yii::app()->db->createCommand()->select('id')->from('jd_sku')->where("sku = '$newSku'")->queryColumn();
if (empty($check)) {
echo "$newSku success\n";
Yii::app()->db->createCommand()->insert('jd_sku', ['sku' => $newSku]);
} else {
echo "$newSku jump\n";
}
// $model = new JdSku();
// $model->sku = $newSku;
// try {
// $model->save();
// Yii::app()->db->createCommand()->insert('jd_sku', ['sku' => $newSku]);
// echo "$newSku success\n";
// } catch (CException $exception) {
// // do nothing
// echo "$newSku fail\n";
// }
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment