/plugin/PBAPI/PBAPI/Request/fopenurl.php

https://bitbucket.org/chamilo/chamilo-ext-repo-photobucket-dev/ · PHP · 75 lines · 35 code · 8 blank · 32 comment · 6 complexity · 172fbc70d5de6dd8f76dd1dfc02f34b6 MD5 · raw file

  1. <?php
  2. use common\libraries\Path;
  3. /**
  4. * Photobucket API
  5. * Fluent interface for PHP5
  6. * fopen url 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. * FOPEN_URL request strategy
  21. * requires fopen url wrappers
  22. *
  23. * @package PBAPI
  24. * @subpackage Request
  25. */
  26. class PBAPI_Request_fopenurl 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. $params = $this->request_params;
  40. //setup context
  41. $params['http']['method'] = $method;
  42. if (empty($params['http']['user_agent']))
  43. $params['http']['user_agent'] = __CLASS__;
  44. //setup context for posts
  45. if ($method == 'POST')
  46. {
  47. if (self :: detectFileUploadParams($params))
  48. {
  49. $boundary = uniqid('xx');
  50. $params['http']['header'] = 'Content-Type: multipart/form-data; boundary=' . $boundary;
  51. $params['http']['content'] = self :: multipartEncodeParams($this->oauth_request->getParameters(), $boundary);
  52. }
  53. else
  54. {
  55. $params['http']['header'] = 'Content-Type: application/x-www-form-urlencoded';
  56. $params['http']['content'] = $this->oauth_request->toPostdata();
  57. }
  58. }
  59. $ctx = stream_context_create($params);
  60. $out = file_get_contents($url, false, $ctx);
  61. if ($out == false)
  62. {
  63. throw new PBAPI_Exception('FOPENURL failed'); //todo exception
  64. }
  65. return $out;
  66. }
  67. }