PHP模拟请求雅虎竞标商品加价
test.php文件
<?php //文件路径 /Servdata/web/test/test.php //该页面可通过浏览器打开 $bidUser = $_GET['bidUser']; $auctionId = $_GET['auctionId']; $bidPrice = $_GET['bidPrice']; $bidNum = $_GET['bidNum']; //获取请求IP $client_ip = get_client_ip(); if(!empty($bidUser) && !empty($auctionId) && !empty($bidPrice) && !empty($bidNum)){ //通过系统以命令行模式调用 cli.php,这里 test.php 与 cli.php 文件在同级目录中 //cli.php 页面中的 echo 语句内容会直接显示在 test.php 打开页面中,也可通过 $res 获取内容打印,$return_status为返回状态 $res = system('/Servdata/server/php7/bin/php cli.php --bidUser=' . $bidUser . ' --auctionId=' . $auctionId . ' --bidPrice=' . $bidPrice . ' --bidNum=' . $bidNum, $return_status); exit; echo $res;exit; var_dump($res); echo '------'; var_dump($return_status); } function get_client_ip() { $ipaddress = ''; if (getenv('HTTP_CLIENT_IP')) $ipaddress = getenv('HTTP_CLIENT_IP'); else if(getenv('HTTP_X_FORWARDED_FOR')) $ipaddress = getenv('HTTP_X_FORWARDED_FOR'); else if(getenv('HTTP_X_FORWARDED')) $ipaddress = getenv('HTTP_X_FORWARDED'); else if(getenv('HTTP_FORWARDED_FOR')) $ipaddress = getenv('HTTP_FORWARDED_FOR'); else if(getenv('HTTP_FORWARDED')) $ipaddress = getenv('HTTP_FORWARDED'); else if(getenv('REMOTE_ADDR')) $ipaddress = getenv('REMOTE_ADDR'); else $ipaddress = 'UNKNOWN'; return $ipaddress; }
cli.php文件
<?php /* 文件路径 /Servdata/web/test/cli.php 该页面只能通过cli命令行打开 有一定几率报 Curl error: Failed connect to auctions.yahoo.co.jp:443; Connection timed out 错误,需要多试几次 服务器上进入cli.php目录 cd /Servdata/web/test 以PHP服务(当前服务器PHP服务所在目录后面跟空格文件名)执行命令 /Servdata/server/php7/bin/php cli.php --bidUser=testuser --auctionId=testauctionid --bidPrice=220 --bidNum=1 '1' => "出价成功,您是最高出价者", '2' => "出价金额无效或已被“暗标”超越,请重新出价", '3' => "卖家设置账户信誉等级要求未满足", '4' => "账户受限制需要手机认证", '5' => "用户余额不足", '8' => "其他账户问题导致出价失败", '9' => "其他未知原因" */ if (strtolower(php_sapi_name()) != 'cli') { echo " error : this script must run by php-cli mode.\n\n\n"; exit; } //出价账号 $bidUser = ""; //商品ID $auctionId = ""; //商品详细页路径 $auctionUrl = "https://page.auctions.yahoo.co.jp/jp/auction/"; //出价金额 $bidPrice = "0"; //投标数量 $bidNum = "1"; //此处两个值作为例子,实际从数据库抽取 $cookie = '';//"YLS=v=2&p=1&n=1; irepNoBidExp=0; irepNoWonExp=0; irepIsLogin=1; irepLastBidTime=0; irepLastWonTime=0; F=a=UVasaT4MvSZvPGvdP0C7MJOcE.ojS4MDOIGrmxPYe7CBfEN.H6MIukZdunLp3TkoYBtPS5gMQxe7T6KLj7DIQBDpF4ut9fM3GYA4yl7huO5wuasxPa2LDGUShk_argaKm4Mq64tZW84gYF2KWe8P907WjJzdXT7JE3zg&b=EOnK; B=e1fuj9lf8c007&b=4&d=fzPQdUJpYF1nfFLHgpIZustNCoNep_lId8MIit_Q&s=sn&i=yY6WBYjTriD9PAcIRva6; XB=e1fuj9lf8c007&b=4&d=fzPQdUJpYF1nfFLHgpIZustNCoNep_lId8MIit_Q&s=sn&i=yY6WBYjTriD9PAcIRva6; Y=v=1&n=7p5c6nk4n3vj3&l=8cie6ii86684/o&p=f24vvjp012000000&ig=01764&r=ui&lg=ja-JP&intl=jp; T=z=XyTleBXaiueB2Dtgev.DE1JMDc3TwY0TjA0NzA2MzE-&sk=DAA2bMrNg2ikl5&ks=EAAp0O1ynZfEb5m13fZyB3aLQ--~F&kt=EAAQB.g.hmUJ7Ib9T.5RCRBaA--~E&ku=FAAMEUCIQCtZTSQg1RoKu414vXP_3AmkpMSItmoU72gFeNYaCySRwIgEaRc2oPYss5PlhKwApgUyyahps7UhnxJ.hQajL5UamQ-~B&d=dGlwAWpFUFVtQQFhAVlBRQFnAU01REVaVVU1TUxMNFpOQldUWE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Mowac5WZb1PV534BfSUfYSIdnKuw8cTAGsO5ntXqzds67hz4QUWtI3LESe5C4YPns_Y5wzCd35KXX-id6fcnu9YB9UuS0TZ1PYOTYX3LCIcBw3RhB-82u6gO1ezCzkc2XXumwInErX1g9hIe7GqoALc2p9yUgMOtHjOWSNcWx3kxrCZ3oaL8o6Vt6f4JAuxzZaHvzI2Jrxa0fF0apxYTGxIocq72aIBtlnIvDzE0pG3AJd3g__g15gOtCEZ1.2; XC=KxQJTGu+7l8bv1VxDae14AMv0fpBGXCIKkDLtHfXZNJGJy+z1K8EGlX+JNIKE1LMIOgeF9u5ImJeRv+elqrmXK02Yjfst3IVO/3hGQKOFHjMp4hNXl77klqti99RdfXMLdwRTh3BPv/iYqPoyku1Tk6dAPFncbvW5rhymdfPVhEOggmUmyUN7HdnEnwtD9ctD0tpTfDfhMpAu/hx26YBaLD5Nk+I1Yimtvy8Mv56UwMiqRIWkvDQ2RskOFhRAHpHW1zmRYaovgCOdQ71MzDLGgZkMQlvdTtHZwC+e+SWjY4EiDwP9O0esv6qV1o9GFLgDA8xMz/JTOOkfVXli30oLkJMwlSIhGeDA5YFYQ8b1Cd7O2+S6EDSpPQl6MRhSnnIYSGiUaQBDdR9M1BXXHhFA8RUOzLrJ7OEP8F3NgaY9kXVJJ7BBh3dhs1FOfGJjH+KSfCdNSpVjYPp/u9C1uI7jF2og3Xmerlq2Ed2bcEZKTDNihXVFQw0Ca46+DkiCKlTe5HnYFIPNEvVhRjYoG67Pb2F/gdfRrxPDE4ZzvzWV2fVHiX1J5BoN7Yvp+wHmyYda5CaJh6kQnxhU3MxbI5wU+SWqZcnNpUxUOBtwh1McR4=.1"; $user_agent = '';//"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"; //从命令行获取参数 foreach ($argv as $k=>$v) { if (strpos($v, '-bidUser=')===1) {// 获取 --bidUser参数 $bidUser = substr($v, 10); } elseif (strpos($v, '-auctionId=')===1) {// 获取 --auctionId参数 $auctionId = substr($v, 12); $auctionUrl .= $auctionId; } elseif (strpos($v, '-bidPrice=')===1) {// 获取 --bidPrice参数 $bidPrice = substr($v, 11); }elseif (strpos($v, '-bidNum=')===1) {// 获取 --bidNum参数 $bidNum = substr($v, 9); } } //========================= begin ======================== //日志路径 $log_file = './log/' . date("Y-m-d") . "cli.log"; $log_txt = "\r\n\r\n\r\n\r\n"; $log_txt .= date("Y-m-d H:i:s") . " 开始请求 \r\n"; $log_txt .= date("Y-m-d H:i:s") . " 请求参数--bidUser : " . $bidUser . "--auctionId : " . $auctionId . "--auctionUrl : " . $auctionUrl . "--bidPrice : " . $bidPrice . "--bidNum : " . $bidNum . "\r\n"; file_put_contents($log_file, $log_txt, FILE_APPEND); //========================= end ======================== //请求数据库,根据不同出价账号获取对应登陆的cookie与user_agent $link = mysqli_connect ( '149.129.80.10:3306', 'root', '123456', 'testdb' ) or die ( "Error " ); mysqli_query ( $link, "SET NAMES UTF8" ) or die ( "Error " . mysqli_error ( $link ) ); $bidUserSql = "select * from biduser where user='$bidUser'"; //========================= begin ======================== $log_txt = "\r\n" . date("Y-m-d H:i:s") . " 请求sql : " . $bidUserSql . " \r\n"; file_put_contents($log_file, $log_txt, FILE_APPEND); //========================= end ======================== $bidUserQuery = mysqli_query ( $link, $bidUserSql ) or die ( "Error " . mysqli_error ( $link ) ); $bidUserRow = mysqli_fetch_array ( $bidUserQuery ); $cookie = $bidUserRow['cookie']; $user_agent = $bidUserRow['user_agent']; //雅虎账号密文(每个账户唯一) $bidders = $bidUserRow['bidders']; //========================= begin ======================== $log_txt = "\r\n" . date("Y-m-d H:i:s") . " sql请求结果json : " . json_encode($bidUserRow) . " \r\n"; file_put_contents($log_file, $log_txt, FILE_APPEND); //========================= end ======================== //设置时区为日本 date_default_timezone_set('Asia/Tokyo'); ini_set('display_errors', 'on'); ini_set('memory_limit', '1024M'); $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSLVERSION, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);//'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0.2 Safari/604.4.7'; curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_COOKIE, $cookie); curl_setopt($curl, CURLOPT_REFERER, $auctionUrl); curl_setopt($curl, CURLOPT_TIMEOUT, 30); //==================================使用代理服务器 //$userName = ""; //$passWord = ""; //CURLOPT_HTTPPROXYTUNNEL : 1启用,启用时会通过HTTP代理来传输。默认0 //curl_setopt($curl, CURLOPT_HTTPPROXYTUNNEL, 1); //CURLOPT_PROXY : 设置通过的HTTP代理服务器。这里是 50.95.131.174:3128 也可以是 proxy.lxvoip.com:1080 这样 //curl_setopt($curl, CURLOPT_PROXY, '50.95.131.174:3128'); //CURLOPT_PROXYUSERPWD : 连接到代理服务器的,格式为“[username]:[password]”的用户名和密码。 //curl_setopt($curl, CURLOPT_PROXYUSERPWD, "$userName:$passWord"); //以下也可以设置代理服务器的地址端口 作用同 CURLOPT_PROXY 一致 //curl_setopt($curl, CURLOPT_PROXY, '50.95.131.174'); //curl_setopt($curl, CURLOPT_PROXYPORT, '3128'); //================================== curl_setopt($curl, CURLOPT_POST, 1); //拼接传值 $curlPostArr = [ 'ItemID' => $auctionId, 'login' => $bidUser, 'cc' => 'jp', 'Quantity' => $bidNum, 'Bid' => $bidPrice ]; curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPostArr); curl_setopt($curl, CURLOPT_URL, 'https://auctions.yahoo.co.jp/jp/show/bid_preview'); //因为错误率,如有错误自动循环5次 for($i = 1; $i < 6; $i++){ $response = curl_exec($curl); //========================= begin ======================== $log_txt = "\r\n" . date("Y-m-d H:i:s") . "第一次".$i."轮curl请求返回结果 : " . $response . " \r\n"; file_put_contents($log_file, $log_txt, FILE_APPEND); //========================= end ======================== // 检查错误 if (curl_errno($curl)) { //如果报错打印报错信息 //========================= begin ======================== $log_txt = "\r\n" . date("Y-m-d H:i:s") . " 第一次".$i."轮curl请求错误 : " . curl_error($curl) . " \r\n"; $log_txt .= "\r\n 提交订单网页投标预览信息错误 : 7 无法访问 bid_preview \r\n"; file_put_contents($log_file, $log_txt, FILE_APPEND); //========================= end ======================== if($i >= 5){ echo 7; exit; } }else{ break; } } //从返回的网页内容中抽取指定信息,作为下次请求参数 $matchers = []; $formField_a = null; preg_match('/<input\s+type="hidden"\s+name="a"\s+value="([\da-zA-Z\_\-\.+=\/\\\]+)">/mis', $response, $matchers); if (isset($matchers[1])) { $formField_a = $matchers[1]; } unset($matchers); if (is_null($formField_a)) { //========================= begin ======================== $log_txt = "\r\n" . date("Y-m-d H:i:s") . " 第一次curl请求正则失败 \r\n"; $log_txt .= "\r\n 提交订单网页投标预览获取信息错误 : 3 formField_a 不存在 \r\n"; file_put_contents($log_file, $log_txt, FILE_APPEND); //========================= end ======================== echo 3; exit; } //========================= begin ======================== $log_txt = "\r\n" . date("Y-m-d H:i:s") . " 正则后结果formField_a : " . $formField_a . " \r\n"; file_put_contents($log_file, $log_txt, FILE_APPEND); //========================= end ======================== //进行二次请求 $curlPostArr = [ 'ItemID' => $auctionId, 'login' => $bidUser, 'cc' => 'jp', 'Quantity' => $bidNum, 'Bid' => $bidPrice, 'a' => $formField_a, 'u' => null, 'bidType' => 100 ]; curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPostArr); curl_setopt($curl, CURLOPT_URL, 'https://auctions.yahoo.co.jp/jp/config/placebid'); //这次可以对 $response 内容进行信息抽取,验证投标成功与否 $response = curl_exec($curl); //========================= begin ======================== $log_txt = "\r\n" . date("Y-m-d H:i:s") . " 二次curl请求结果 : " . $response . " \r\n"; file_put_contents($log_file, $log_txt, FILE_APPEND); //========================= end ======================== // 检查错误 if (curl_errno($curl)) { //========================= begin ======================== $log_txt = "\r\n" . date("Y-m-d H:i:s") . " 第二次curl请求错误 : " . curl_error($curl) . " \r\n"; $log_txt .= "\r\n 投标失败 : 4 \r\n"; file_put_contents($log_file, $log_txt, FILE_APPEND); //========================= end ======================== //如果报错打印报错信息 echo 4; exit; } //获取最新报价信息 $json = file_get_contents("http://bms.shaogood.com/index/order_bid/getauctioninfo?auction_id=" . $auctionId); $auctioninfo = json_decode($json, true); //验证当前最高价是否是出价账户 if($auctioninfo['highest_bidders'] == $bidders){ //========================= begin ======================== $log_txt = "\r\n" . date("Y-m-d H:i:s") . " 投标成功 : 1" . " \r\n"; file_put_contents($log_file, $log_txt, FILE_APPEND); //========================= end ======================== //echo "\n\n\n投标完成\n\n\n"; echo 1; }else{ //========================= begin ======================== $log_txt = "\r\n" . date("Y-m-d H:i:s") . " 投标失败 : 6 投标暗号" . $bidders . " --当前最高价暗号" . $auctioninfo['highest_bidders'] . " \r\n"; file_put_contents($log_file, $log_txt, FILE_APPEND); //========================= end ======================== echo 6; }