PageRenderTime 65ms CodeModel.GetById 53ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 2use 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/**
17 * Load Request parent
18 */
19require_once dirname(__FILE__) . '/../Request.php';
20/**
21 * FOPEN_URL request strategy
22 * requires fopen url wrappers
23 * 
24 * @package PBAPI
25 * @subpackage Request
26 */
27class PBAPI_Request_fopenurl extends PBAPI_Request
28{
29
30    /**
31     * Do actual request
32     *
33     * @param string $method
34     * @param string $uri
35     * @param array $params
36     * @return string
37     */
38    protected function request($method, $uri, $params = array())
39    {
40        $url = $this->preRequest($method, $uri, $params);
41        
42        $params = $this->request_params;
43        
44        //setup context
45        $params['http']['method'] = $method;
46        if (empty($params['http']['user_agent']))
47            $params['http']['user_agent'] = __CLASS__;
48        
49     //setup context for posts
50        if ($method == 'POST')
51        {
52            if (self :: detectFileUploadParams($params))
53            {
54                $boundary = uniqid('xx');
55                $params['http']['header'] = 'Content-Type: multipart/form-data; boundary=' . $boundary;
56                $params['http']['content'] = self :: multipartEncodeParams($this->oauth_request->getParameters(), $boundary);
57            }
58            else
59            {
60                $params['http']['header'] = 'Content-Type: application/x-www-form-urlencoded';
61                $params['http']['content'] = $this->oauth_request->toPostdata();
62            }
63        }
64        
65        $ctx = stream_context_create($params);
66        $out = file_get_contents($url, false, $ctx);
67        
68        if ($out == false)
69        {
70            throw new PBAPI_Exception('FOPENURL failed'); //todo exception
71        }
72        return $out;
73    }
74
75}