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.

102 lines
3.5 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. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  9. if($include_headers) {
  10. curl_setopt($ch, CURLOPT_HEADER, true);
  11. $response = curl_exec($ch);
  12. $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  13. return [
  14. 'status' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
  15. 'headers' => trim(substr($response, 0, $header_size)),
  16. 'body' => substr($response, $header_size)
  17. ];
  18. } else {
  19. return curl_exec($ch);
  20. }
  21. }
  22. function get_head($url) {
  23. $ch = curl_init($url);
  24. set_user_agent($ch);
  25. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  26. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  27. curl_setopt($ch, CURLOPT_HEADER, true);
  28. curl_setopt($ch, CURLOPT_NOBODY, true);
  29. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  30. $headers = curl_exec($ch);
  31. return [
  32. 'status' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
  33. 'headers' => trim($headers)
  34. ];
  35. }
  36. function post($url, $params, $format='form', $headers=[]) {
  37. $ch = curl_init($url);
  38. set_user_agent($ch);
  39. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  40. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  41. if($format == 'json') {
  42. $body = json_encode($params);
  43. $headers[] = 'Content-type: application/json';
  44. } elseif($format == 'form') {
  45. $body = http_build_query($params);
  46. } else {
  47. $body = $params;
  48. }
  49. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  50. curl_setopt($ch, CURLOPT_POST, true);
  51. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
  52. curl_setopt($ch, CURLOPT_HEADER, true);
  53. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  54. $response = curl_exec($ch);
  55. $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  56. return [
  57. 'status' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
  58. 'headers' => trim(substr($response, 0, $header_size)),
  59. 'body' => substr($response, $header_size)
  60. ];
  61. }
  62. function parse_headers($headers) {
  63. $retVal = array();
  64. $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $headers));
  65. foreach($fields as $field) {
  66. if(preg_match('/([^:]+): (.+)/m', $field, $match)) {
  67. $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($m) {
  68. return strtoupper($m[0]);
  69. }, strtolower(trim($match[1])));
  70. // If there's already a value set for the header name being returned, turn it into an array and add the new value
  71. $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($m) {
  72. return strtoupper($m[0]);
  73. }, strtolower(trim($match[1])));
  74. if(isset($retVal[$match[1]])) {
  75. if(!is_array($retVal[$match[1]]))
  76. $retVal[$match[1]] = array($retVal[$match[1]]);
  77. $retVal[$match[1]][] = $match[2];
  78. } else {
  79. $retVal[$match[1]] = trim($match[2]);
  80. }
  81. }
  82. }
  83. return $retVal;
  84. }
  85. function set_user_agent(&$ch) {
  86. // 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
  87. 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');
  88. }
  89. function response_is($status, $prefix) {
  90. if($status) {
  91. $status_str = (string)$status;
  92. return $status_str[0] == (string)$prefix;
  93. } else {
  94. return false;
  95. }
  96. }