解决的是guzzle的cookie生成,保存,使用,用于登录采集
引用文章:
http://guzzle-cn.readthedocs.org/zh_CN/latest/quickstart.html#cookies
https://github.com/guzzle/guzzle
http://blog.sarabande.jp/page/29?route=%2Fpage%2F%3Apage
$url='http://localhost/test.php';
$client=new\GuzzleHttp\Client(['cookies'=>true]);
$request=new\GuzzleHttp\Psr7\Request('GET', $url);
$promise=$client->sendAsync($request)->then(function ($response) {
echo $response->getBody();
});
$promise->wait();
$promise=$client->sendAsync($request)->then(function ($response) {
echo $response->getBody();
});
$promise->wait();
function create()
{
$cookie_str="XSRF-TOKEN=eyJpdiI6InhicXdZTE95ajJ5ejUwNmp1VjNTMHc9PSIsInZhbHVlIjoiY0I3TDVXVUw2Mm9pZGpaOFhOME1OWHZoWFZlZXdvUWZ0bk5FQUh5Y3h1UVRkeWlFeTE4WVVwQStyaG9aREdDTE5pMnI5MXA3QVA0TGNhSElCemo2U2c9PSIsIm1hYyI6Ijk0Mzg1Mzg5OTJjMjYzODEyMWUwMzZlMTUyYWFkN2NiOGY3MjY2ZmUyYTI1OGRiYjczZWIyZTIxNjY0NDA3NDkifQ%3D%3D; laravel_session=eyJpdiI6Ijlha0R4QVg2RnZpbUtPekNtVGU1a1E9PSIsInZhbHVlIjoiUytmekdkQXZMSTdic0FvWFlHQjU5ZkZabTF1NldnU3Bka2hmdUcwT0VWdDVxcWpuQnVsclM0XC9GVEJSUUYwVHFzU3R6dGFqT0ZjQ1RBNmFVcVRHdHpRPT0iLCJtYWMiOiJiZGVkODI1OTMxNmFlMWFjZWZlYzg1Y2E1ODIxMGQ3NDMyNjI5NDE0YjMyZmNjMTM2NDFlODBiOWY5MTliZDViIn0%3D";
$client = new Client(['cookies' => true]);
$response = $client->get('http://www.a031.com/admin/access/users', [
'curl' => [
CURLOPT_COOKIE => $cookie_str
]
]);
var_dump((string) $response->getBody());
exit();
$coockieJar = new CookieJar();
$client = new Client();
$jar =$coockieJar->fromArray(['XSRF-TOKEN' => 'eyJpdiI6IkJIVFFxVlpPSlQ2NGEyTUdqaXRJQWc9PSIsInZhbHVlIjoiK3RCQnBDSDJ1XC9IMW1aa00zTHZ2VzREVmxEbFwvaGhyamxcLzR6eHl0YW9nWXdaVmE2M3NIVEZlUjZxY095YjFxRzdtaVowVXI0aXp1cjYzTTU5ZHE4VWc9PSIsIm1hYyI6IjAwZjI0ZGM4NTZjM2Q2ZWY0MmIwMTE3YmIyZTQwY2M5NWE1ZDVhMzk4YmE0NTRmMWZlYmQ5ZWU1OTk3NjkxNWQifQ%3D%3D','laravel_session' => 'eyJpdiI6IkExY25FeTVDT0hMU2luS25xVWdKbWc9PSIsInZhbHVlIjoiTGVCSHNxQVwvUk5BeER4WXJWMUc3dGRLeENUalJNNTJOMnNNRWtsbUlOM0tKeThFZ3dMNkZzXC8yTWFQbWh5R2JzNFwvXC9McWRkMVQxN3VTXC9ubnoxQkVUUT09IiwibWFjIjoiZTdiZjU4NDgxZWQyMDdmMjlhOGY3ZjMxN2Q3YzE5NzFlYWI1NWUwOGMxNzVjZTA0NDkwY2UwZmFhNzRmNzM1ZiJ9'], 'www.a031.com');
$response = $client->get('http://www.a031.com/admin/access/users', [
'cookies' => $jar
]);
var_dump((string) $response->getBody());
exit();
$cookie_str='laravel_session=eyJpdiI6InVMK2dFMndTVkZLK3NMVDdrdCtyZFE9PSIsInZhbHVlIjoiVU42VWJmdkwzeTFNbWFMTE80cHdvOHh6VTRmcHVtait6RnorbFRDMUhBR1NsR0VTWDhUV3puRE1yWjB2K0tHNmM4ZVpjNk5Db25uRnRCSWZlcllvanc9PSIsIm1hYyI6IjA2Yjg0NTNhYzFkMTQyNzcxNjM5MmVmYTJjMWIxNmNmYmY4ZjMwNTYwNWE5MWZjNmYxNjQyMWIzN2RkNmQ4MjIifQ%3D%3D; expires=Fri, 15-Apr-2016 10:27:52 GMT; Max-Age=7200; path=/; httponly';
$handler = HandlerStack::create();
$handler->push(Middleware::mapRequest(function (RequestInterface $request) {
return $request->withHeader('XSRF-TOKEN', 'eyJpdiI6IlNpOXplVWRxVTd1WnpMRWpCSnExYXc9PSIsInZhbHVlIjoicmMyc3BqMnpMYzJlTGE1S3JjT2hQaldBbXNKOEljY3E1YjVtZk9Oa2JmV1pOMHQyMlpLcFFnK1R0djczQzBhQktKdndUcWZUc0tOY2xuNUpCcFpWSnc9PSIsIm1hYyI6IjRlMGYyNmNlNGNmN2I5NmQ1ZmFlOGNkMGQ0OWQ0NDQxYTRjMmFlOWZkZjAzYjVhM2I4MWMyYzY2N2M5ZDgxYjQifQ%3D%3D');
}));
$handler->push(Middleware::mapRequest(function (RequestInterface $request) {
return $request->withHeader('laravel_session', 'eyJpdiI6IkYzM29kbVg0QWxrQVJEZzJDWHp5Rmc9PSIsInZhbHVlIjoic25lM3ZKWFFTTmVYeHpodGx1cXpjVXIxZU5wdjNwQnhnQ09cL2FtcVBSRFJwcnFYM0hIODVHeVhXOTU0THJqS3NGbzVOZkV1MjNmZjlRcHBsZXRJTzRnPT0iLCJtYWMiOiIxZGRhMTcwYWQ3MjRkMTZjNDQxMGFhYTk5OTJlYjg2ZDVkNGY3Yzk5MjBjNzMwM2FmNjU4OTIxYjg2MTM4Y2VmIn0%3D');
}));
$client = new Client(['handler' =>$handler]);
$url='http://www.a031.com/admin/access/users';
$response = $client->request('GET', $url, ['cookies' => $coockieJar]);
$code = $response->getStatusCode();
$body = $response->getBody();
$headers=$response->getHeaders();
$remainingBytes = $body->getContents();
print_r($headers);
exit();
$cookie_str='__c_review=2; __c_last=1457509091713; __c_visitor=1455686505901117; XSRF-TOKEN=eyJpdiI6Ino1N3BwYjdEZ20wakdpbWFBSG9JTWc9PSIsInZhbHVlIjoiUzNxa0Y4QUsyaG5URlhtTjhkZGJwMWtUTDdyMUlFVDRVSlMzZ2xoWGlyVVJDM0sxMllmZ1U1dHM2dlhUT25MV25kV2RcL2lLdzF3bU1NK3Y3dW1pS2J3PT0iLCJtYWMiOiJiZmUyODA0YjQ2ZWRjMzRhYWNhNGUzZDdmNzA3NTMyZGY0OGI0OTU2Nzc4NDQ1NzQxNGVjNWNiNDgyZjA2ODY1In0%3D';
$coockieJar = new CookieJar();
$coockieJar->setCookie(SetCookie::fromString($cookie_str));
$client = new Client();
$url='http://www.a031.com/admin/access/users';
$response = $client->request('GET', $url, ['cookies' => $coockieJar]);
$code = $response->getStatusCode();
$body = $response->getBody();
$remainingBytes = $body->getContents();
print_r($remainingBytes);
exit();
$cookie_str='t=d3c8ce3bcabf029e666c6125ff84b7f2; lzstat_uv=528220081126903958|1774292@1774054; cna=2Gc4D3SaOBECATFIK5ciVqN0; mm-guidance3=1; _umdata=CBCF5C0AD3B0C709FFCA0C2951A531866694F33CCCAE949D3350385CB1D4672E8B532E6FF3A66AAAADCC8C6502C11E8F7A1E79CE354744285FADEFFEE83439C97A2BFE35AEAA140141CDA91201B5830629D8288F844809E9CA1ECF701CC83A19; _tb_token_=4Jo7TyDVpQp; v=0; cookie32=fae809d0d1dd664c2d047cc94e701a74; cookie31=MzM1MjkyNzQsJUU5JUExJUI2JUU1JTkxJUIxJUU1JTkxJUIxJUU1JTkzJTgxJUU3JTg5JThDJUU1JUJBJTk3JUU5JTkzJUJBLDE3MzA3NTgxMTdAUVEuQ09NLFRC; login=WqG3DMC9VAQiUQ%3D%3D; l=AsbGryILvnIofOnVvXg1yYDnFrJIIgrh';
$mock = new MockHandler([
new Response(301, [
'Location' => 'http://pub.alimama.com',
'Set-Cookie' => $cookie_str
]),
new Response(200)
]);
$handler = HandlerStack::create($mock);
$url='http://pub.alimama.com/event/squareList.json?spm=a2320.7388781.a214tr8.d006.ZwPuwP&toPage=1&eventCat=1&commissionRange=4&eventStatus=5&highQuality=-1&orderType=3&key=&perPageSize=10&platformType=-1&catId=1&commissionRangeType=4&promotionType=-1&t=1460637555760&pvid=&_tb_token_=4Jo7TyDVpQp&_input_charset=utf-8';
$request = new Request('GET', $url);
$jar = new CookieJar();
$response = $handler($request, [
'allow_redirects' => true,
'cookies' => $jar
])->wait();
$body = $response->getBody();
$remainingBytes = $body->getContents();
print_r($remainingBytes);
exit();
这部分应该是
$coockieJar = new CookieJar();
$coockieJar->setCookie(SetCookie::fromString($cookie_str));
$client = new Client();
$url='http://pub.alimama.com/event/squareList.json?spm=a2320.7388781.a214tr8.d006.ZwPuwP&toPage=1&eventCat=1&commissionRange=4&eventStatus=5&highQuality=-1&orderType=3&key=&perPageSize=10&platformType=-1&catId=1&commissionRangeType=4&promotionType=-1&t=1460637555760&pvid=&_tb_token_=4Jo7TyDVpQp&_input_charset=utf-8';
$response = $client->request('GET', $url, ['cookies' => $coockieJar]);
$code = $response->getStatusCode();
$body = $response->getBody();
$remainingBytes = $body->getContents();
print_r($remainingBytes);
exit();
$cookie_str='t=d3c8ce3bcabf029e666c6125ff84b7f2; lzstat_uv=528220081126903958|1774292@1774054; cna=2Gc4D3SaOBECATFIK5ciVqN0; mm-guidance3=1; v=0; _tb_token_=i8CBukXqmQp; _umdata=CBCF5C0AD3B0C709FFCA0C2951A531866694F33CCCAE949D3350385CB1D4672E8B532E6FF3A66AAAADCC8C6502C11E8F7A1E79CE354744285FADEFFEE83439C97A2BFE35AEAA140141CDA91201B5830629D8288F844809E9CA1ECF701CC83A19; cookie32=fae809d0d1dd664c2d047cc94e701a74; cookie31=MzM1MjkyNzQsJUU5JUExJUI2JUU1JTkxJUIxJUU1JTkxJUIxJUU1JTkzJTgxJUU3JTg5JThDJUU1JUJBJTk3JUU5JTkzJUJBLDE3MzA3NTgxMTdAUVEuQ09NLFRC; login=U%2BGCWk%2F75gdr5Q%3D%3D; l=Ajc32-Zkf63xp/hiJDOUJmvGxyWB/Ate';
$coockieJar = new CookieJar();
$coockieJar->setCookie(SetCookie::fromString($cookie_str));
$this->loginMade = true;
$stack = HandlerStack::create();
$middleware =Middleware::cookies();
$stack->push($middleware);
$client = new Client(['base_uri'=>'http://www.alimama.com','timeout' => 10.0,'handler'=>$stack]);
$url='http://pub.alimama.com/event/squareList.json?spm=a2320.7388781.a214tr8.d006.ZwPuwP&toPage=1&eventCat=1&commissionRange=4&eventStatus=5&highQuality=-1&orderType=3&key=&perPageSize=10&platformType=-1&catId=1&commissionRangeType=4&promotionType=-1&t=1460637555760&pvid=&_tb_token_=i8CBukXqmQp&_input_charset=utf-8';
$response = $client->request('GET', $url, ['cookies' => $coockieJar]);
$code = $response->getStatusCode();
// $body = $response->getHeaders();
$body = $response->getBody();
// $stringBody = (string) $body;
$remainingBytes = $body->getContents();
print_r($remainingBytes);
exit();
$cookie_str='t=d3c8ce3bcabf029e666c6125ff84b7f2; lzstat_uv=528220081126903958|1774292@1774054; cna=2Gc4D3SaOBECATFIK5ciVqN0; mm-guidance3=1; _umdata=CBCF5C0AD3B0C709FFCA0C2951A531866694F33CCCAE949D3350385CB1D4672E8B532E6FF3A66AAAADCC8C6502C11E8F7A1E79CE354744285FADEFFEE83439C97A2BFE35AEAA140141CDA91201B5830629D8288F844809E9CA1ECF701CC83A19; v=0; _tb_token_=i8CBukXqmQp; l=AicnCx95b51hF-hStOPkduRDt1XxrPuO';
$cookie_class=new setCookie();
$cookie_obj=$cookie_class->fromString($cookie_str);
// $cookie_obj->setDomain('.jd.com');
$cookie_arr=$cookie_obj->toArray();
// $_tb_token_=$cookie_arr['_tb_token_'];
$client = new Client(['base_uri'=>'https://media.jd.com/index/overview','timeout' => 10.0]);
$jar = new \GuzzleHttp\Cookie\CookieJar();
$jar->setCookie($cookie_obj);
$response = $client->request('GET', 'https://media.jd.com/index/overview', [
'cookies' =>$jar,
'verify'=>false
]);
// dd($jar);
// dd($response->getUri());
$body = $response->getBody();
$remainingBytes = $body->getContents();
print_r($remainingBytes);
exit();
$url='http://pub.alimama.com/event/squareList.json?spm=a2320.7388781.a214tr8.d006.ZwPuwP&toPage=1&eventCat=1&commissionRange=4&eventStatus=5&highQuality=-1&orderType=3&key=&perPageSize=10&platformType=-1&catId=1&commissionRangeType=4&promotionType=-1&t=1460637555760&pvid=&_tb_token_=QlR7AH1ihQp&_input_charset=utf-8';
$response = $client->request('GET', $url, [
'cookies' =>$jar
]);
$code = $response->getStatusCode();
// $body = $response->getHeaders();
$body = $response->getBody();
// $stringBody = (string) $body;
$remainingBytes = $body->getContents();
print_r($remainingBytes);
exit();
// Create a new cookie plugin
$cookiePlugin = new CookiePlugin(new ArrayCookieJar());
// Add the cookie plugin to the client
$client->addSubscriber($cookiePlugin);
exit();
$client->addSubscriber($cookie);
$client = new Client();
$request = $client->get('http://pub.alimama.com/event/squareList.json?spm=a2320.7388781.a214tr8.d006.5hQVJV&toPage=1&promotionType=-1&eventCat=1&commissionRange=2&eventStatus=5&highQuality=-1&perPageSize=100&orderType=3&key=&platformType=-1&catId=1&commissionRangeType=2&t=1460606594020&pvid=&_tb_token_=n68A1AqIdQp&_input_charset=utf-8');
$response = $request->send();
$status=$response->getStatusCode();
dd($status);
$body=$response->getBody();
dd($status);
}