Web.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. /**
  8. * Description of Http
  9. *
  10. * @author jgao
  11. */
  12. class Web {
  13. //put your code here
  14. static $lastError = 0;
  15. static function request($url, $data){
  16. $ch = curl_init();
  17. curl_setopt($ch, CURLOPT_URL, $url);
  18. curl_setopt($ch, CURLOPT_POST, true);
  19. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  20. curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,true);
  21. curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
  22. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  23. curl_setopt($ch, CURLOPT_MAXREDIRS,10);
  24. curl_setopt($ch, CURLOPT_TIMEOUT,10);
  25. curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  26. $output = curl_exec($ch);
  27. self::$lastError = curl_errno($ch);
  28. curl_close($ch);
  29. return $output;
  30. }
  31. static function getQueryString() {
  32. $queryStr = "";
  33. switch ($_SERVER['REQUEST_METHOD']) {
  34. case 'GET':
  35. $queryStr = $_SERVER["QUERY_STRING"];
  36. break;
  37. case 'POST':
  38. $queryStr = file_get_contents("php://input");
  39. break;
  40. }
  41. return $queryStr;
  42. }
  43. /**
  44. * 提取url参数
  45. * @param type $queryStr
  46. * @return type
  47. */
  48. static function getQueryParas() {
  49. return self::parseQueryParas(self::getQueryString());
  50. }
  51. static function parseQueryParas($queryStr) {
  52. $arr = explode("&", $queryStr);
  53. $queryParas = array();
  54. foreach ($arr as $value) {
  55. $paras = explode("=", $value);
  56. $queryParas[$paras[0]] = $paras[1];
  57. }
  58. return $queryParas;
  59. }
  60. }