You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.4 KiB

  1. <?php
  2. namespace request;
  3. function get_url($url, $include_headers=false) {
  4. $ch = curl_init($url);
  5. set_user_agent($ch);
  6. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  7. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  8. if($include_headers) {
  9. curl_setopt($ch, CURLOPT_HEADER, true);
  10. $response = curl_exec($ch);
  11. $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  12. return [
  13. 'status' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
  14. 'headers' => trim(substr($response, 0, $header_size)),
  15. 'body' => substr($response, $header_size)
  16. ];
  17. } else {
  18. return curl_exec($ch);
  19. }
  20. }
  21. function get_head($url) {
  22. $ch = curl_init($url);
  23. set_user_agent($ch);
  24. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  25. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  26. curl_setopt($ch, CURLOPT_HEADER, true);
  27. curl_setopt($ch, CURLOPT_NOBODY, true);
  28. $headers = curl_exec($ch);
  29. return [
  30. 'status' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
  31. 'headers' => trim($headers)
  32. ];
  33. }
  34. function post($url, $params, $format='form', $headers=[]) {
  35. $ch = curl_init($url);
  36. set_user_agent($ch);
  37. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  38. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  39. if($format == 'json') {
  40. $body = json_encode($params);
  41. $headers[] = 'Content-type: application/json';
  42. } elseif($format == 'form') {
  43. $body = http_build_query($params);
  44. } else {
  45. $body = $params;
  46. }
  47. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  48. curl_setopt($ch, CURLOPT_POST, true);
  49. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
  50. curl_setopt($ch, CURLOPT_HEADER, true);
  51. $response = curl_exec($ch);
  52. $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  53. return [
  54. 'status' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
  55. 'headers' => trim(substr($response, 0, $header_size)),
  56. 'body' => substr($response, $header_size)
  57. ];
  58. }
  59. function parse_headers($headers) {
  60. $retVal = array();
  61. $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $headers));
  62. foreach($fields as $field) {
  63. if(preg_match('/([^:]+): (.+)/m', $field, $match)) {
  64. $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($m) {
  65. return strtoupper($m[0]);
  66. }, strtolower(trim($match[1])));
  67. // If there's already a value set for the header name being returned, turn it into an array and add the new value
  68. $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($m) {
  69. return strtoupper($m[0]);
  70. }, strtolower(trim($match[1])));
  71. if(isset($retVal[$match[1]])) {
  72. if(!is_array($retVal[$match[1]]))
  73. $retVal[$match[1]] = array($retVal[$match[1]]);
  74. $retVal[$match[1]][] = $match[2];
  75. } else {
  76. $retVal[$match[1]] = trim($match[2]);
  77. }
  78. }
  79. }
  80. return $retVal;
  81. }
  82. function set_user_agent(&$ch) {
  83. // Unfortunately I've seen a bunch of websites return different content when the user agent is set to something like curl or other server-side libraries, so we have to pretend to be a browser to successfully get the real HTML
  84. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) p3k/Switchboard/0.1.1 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36');
  85. }
  86. function response_is($status, $prefix) {
  87. if($status) {
  88. $status_str = (string)$status;
  89. return $status_str[0] == (string)$prefix;
  90. } else {
  91. return false;
  92. }
  93. }