/plugin/PBAPI/PBAPI/Request/fsockopen.php

https://bitbucket.org/chamilo/chamilo-ext-repo-photobucket-dev/ · PHP · 103 lines · 56 code · 12 blank · 35 comment · 9 complexity · 810f34b89f79c50cddd1c260c667af2c MD5 · raw file

  1. <?php
  2. use common\libraries\Path;
  3. /**
  4. * Photobucket API
  5. * Fluent interface for PHP5
  6. * fsockopen request method
  7. *
  8. * @author Photobucket
  9. * @package PBAPI
  10. * @subpackage Request
  11. *
  12. * @copyright Copyright Copyright (c) 2008, Photobucket, Inc.
  13. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  14. */
  15. /**
  16. * Load Request parent
  17. */
  18. require_once dirname(__FILE__) . '/../Request.php';
  19. /**
  20. * fsockopen request strategy
  21. * requires ability to use fsockopen
  22. *
  23. * @package PBAPI
  24. * @subpackage Request
  25. */
  26. class PBAPI_Request_fsockopen extends PBAPI_Request
  27. {
  28. /**
  29. * Do actual request
  30. *
  31. * @param string $method
  32. * @param string $uri
  33. * @param array $params
  34. * @return string
  35. */
  36. protected function request($method, $uri, $params = array())
  37. {
  38. $url = $this->preRequest($method, $uri, $params);
  39. $parts = parse_url($url);
  40. $len = 0;
  41. $data = '';
  42. $resp = '';
  43. //open socket
  44. if ($fp = @fsockopen($parts['host'], 80))
  45. {
  46. //generate request headers
  47. $path = (! empty($parts['path'])) ? $parts['path'] : '';
  48. $query = (! empty($parts['query'])) ? '?' . $parts['query'] : '';
  49. fputs($fp, "$method $path$query HTTP/1.1\n");
  50. fputs($fp, "Host: {$parts['host']}\n");
  51. fputs($fp, 'User-Agent: ' . __CLASS__ . "\n");
  52. //generate request headers for post
  53. if ($method == 'POST')
  54. {
  55. if (self :: detectFileUploadParams($params))
  56. {
  57. $boundary = uniqid('xx');
  58. $data = self :: multipartEncodeParams($this->oauth_request->getParameters(), $boundary);
  59. fputs($fp, "Content-Type: multipart/form-data; boundary=$boundary\n");
  60. }
  61. else
  62. {
  63. $data = $this->oauth_request->toPostdata();
  64. fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
  65. }
  66. $len = strlen($data);
  67. fputs($fp, "Content-length: $len\n\n");
  68. fputs($fp, "$data\n");
  69. }
  70. //put last newline to signal i'm done
  71. fputs($fp, "\n");
  72. $headers = true;
  73. while (! feof($fp))
  74. {
  75. $line = fgets($fp); //get lines
  76. if (trim($line) == '')
  77. $headers = false; //empty line will signal that we're done with headers
  78. else
  79. if (! $headers)
  80. $resp .= $line; //dont capture headers to response
  81. }
  82. //close socket
  83. fclose($fp);
  84. }
  85. else
  86. {
  87. throw new PBAPI_Exception('FSOCKOPEN failed'); //todo exception
  88. }
  89. return $resp;
  90. }
  91. }