QueryList post($url,$args = null,$otherArgs = [])


Http post插件,用于post表单。默认已经开启了与get()方法共享cookie 。

postJson()方法用法同post()方法.

用法


用法同http get插件

  1. $ql->post('http://httpbin.org/post',[
  2. 'param1' => 'testvalue',
  3. 'params2' => 'somevalue'
  4. ],[
  5. 'proxy' => 'http://222.141.11.17:8118',
  6. 'timeout' => 30,
  7. 'headers' => [
  8. 'Referer' => 'https://querylist.cc/',
  9. 'User-Agent' => 'testing/1.0',
  10. 'Accept' => 'application/json',
  11. 'X-Foo' => ['Bar', 'Baz'],
  12. 'Cookie' => 'abc=111;xxx=222'
  13. ]
  14. ]);
  15. echo $ql->getHtml();

输出:

  1. {
  2. "args": {},
  3. "data": "",
  4. "files": {},
  5. "form": {
  6. "param1": "testvalue",
  7. "params2": "somevalue"
  8. },
  9. "headers": {
  10. "Accept": "application/json",
  11. "Connection": "close",
  12. "Content-Length": "34",
  13. "Content-Type": "application/x-www-form-urlencoded",
  14. "Cookie": "abc=111;xxx=222",
  15. "Host": "httpbin.org",
  16. "Proxy-Connection": "Keep-Alive",
  17. "Referer": "https://querylist.cc/",
  18. "User-Agent": "testing/1.0",
  19. "X-Foo": "Baz"
  20. },
  21. "json": null,
  22. "origin": "222.141.11.17",
  23. "url": "http://httpbin.org/post"
  24. }

连贯操作

http插件默认已经开启了全局cookie,post操作和get操作是cookie共享的,意味着你可以先调用post方法登录,然后get方法就可以采集所有登录后的页面。

  1. $ql = QueryList::post('http://xxxx.com/login',[
  2. 'username' => 'admin',
  3. 'password' => '123456'
  4. ])->get('http://xxx.com/admin');
  5. $ql->get('http://xxx.com/admin/page');
  6. //echo $ql->getHtml();

实战:模拟登陆GitHub

  1. $ql = QueryList::getInstance();
  2. //手动设置cookie
  3. $jar = new \GuzzleHttp\Cookie\CookieJar();
  4. //获取到登录表单
  5. $form = $ql->get('https://github.com/login',[],[
  6. 'cookies' => $jar
  7. ])->find('form');
  8. //填写GitHub用户名和密码
  9. $form->find('input[name=login]')->val('your github username or email');
  10. $form->find('input[name=password]')->val('your github password');
  11. //序列化表单数据
  12. $fromData = $form->serializeArray();
  13. $postData = [];
  14. foreach ($fromData as $item) {
  15. $postData[$item['name']] = $item['value'];
  16. }
  17. //提交登录表单
  18. $actionUrl = 'https://github.com'.$form->attr('action');
  19. $ql->post($actionUrl,$postData,[
  20. 'cookies' => $jar
  21. ]);
  22. //判断登录是否成功
  23. // echo $ql->getHtml();
  24. $userName = $ql->find('.header-nav-current-user>.css-truncate-target')->text();
  25. if($userName)
  26. {
  27. echo '登录成功!欢迎你:'.$userName;
  28. }else{
  29. echo '登录失败!';
  30. }

输出:

  1. 登录成功!欢迎你:jae-jae