Example #1
0
 /**
  * builds a url usable for a GET request
  */
 public function to_url()
 {
     $post_data = $this->to_post_data();
     $out = \Zeflasher\OAuth\OAuthUtil::get_normalized_http_url($this->_http_url);
     if ($post_data)
     {
         $out .= '?'.$post_data;
     }
     return $out;
 }
Example #2
0
    /**
     * The return object is as follow
     * {
     * string header The response header
     * string body The response body
     * int http_code The Http response code
     * string last_url The last url called
     * }
     * @param string $url All the query string will be removed. Passed them in the param array
     * @param string $method GET, POST, PUT, DELETE, and any custom ones you would provide
     * @param array $parameters Array in the following format ['key' => value], if set will override the existing ones set using set_parameter
     * @param string $returnType Format we you are expecting the response (not implemented yet)
     * @param string $callbackUrl The callbackurl (optional)
     * @param string $verifier Any verifier (optional)
     * @param string $proxy Proxy to use
     * @return \stdClass
     */
    public function request($url, $method = 'GET', Array $parameters = null, $returnType = 'json', $callbackUrl = null, $verifier = null, $proxy = null)
    {
        $this->_http_url = $url;
        $this->_http_method = $method;
        if( !is_null($parameters) )
        {
            $this->_parameters = $parameters;
        }

        if (isset ($callbackUrl))
        {
            $this->_parameters['oauth_callback'] = $callbackUrl;
        }

        if (isset ($verifier))
        {
            $this->_parameters['oauth_verifier'] = $verifier;
        }

        //  add the OAuth parameters
        $this->createOAuthParameters();
        $this->sign_request($this->_signature_method, $this->_consumer_secret, $this->_token_secret);

        //  create the curl options
        $curl_options = array
        (
            CURLOPT_URL => \Zeflasher\OAuth\OAuthUtil::get_normalized_http_url($this->_http_url),
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => false
        );

        if ($proxy)
        {
            $curl_options[CURLOPT_PROXY] = $proxy;
        }

        switch ($method)
        {
            //  get
            case \Zeflasher\OAuth\OAuthConstants::OAUTH_HTTP_METHOD_GET:
                $curl_options[CURLOPT_HTTPGET] = 1;
                $curl_options[CURLOPT_URL] = $this->to_url();
                break;

            //  set the post parameters if post method
            case \Zeflasher\OAuth\OAuthConstants::OAUTH_HTTP_METHOD_POST:
                $curl_options[CURLOPT_POST] = 1;
                $curl_options[CURLOPT_POSTFIELDS] = $this->get_query_parameters();
                break;

            //  set the put parameters if put method
            case \Zeflasher\OAuth\OAuthConstants::OAUTH_HTTP_METHOD_PUT:
                $curl_options[CURLOPT_CUSTOMREQUEST] = 'PUT';
                $curl_options[CURLOPT_POSTFIELDS] = $this->get_query_parameters();
                break;

            //  set the put parameters if put method
            case \Zeflasher\OAuth\OAuthConstants::OAUTH_HTTP_METHOD_DELETE:
                $curl_options[CURLOPT_CUSTOMREQUEST] = 'DELETE';
                $curl_options[CURLOPT_POSTFIELDS] = $this->get_query_parameters();
                break;
        }

        //  Add the headers
        $curl_options[CURLOPT_HTTPHEADER] = array($this->to_header());
        $curl_options[CURLOPT_HEADER] = true;
        $curl_options[CURLOPT_FOLLOWLOCATION] = true;
        $curl_options[CURLOPT_TIMEOUT] = $this->_timeout;
        $curl_options[CURLOPT_CONNECTTIMEOUT] = $this->_connection_timeout;


        // make CURL request
        $curl = curl_init();
        curl_setopt_array($curl, $curl_options);
        $response = curl_exec($curl);
        $error = curl_error($curl);
        $result = new \stdClass();

        if ($error != "")
        {
            $result->errors = array();
            $error = new \stdClass();
            $error->message = $error;
            $error->message_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
            array_push($result->errors, $error);
            return $result;
        }

        $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
        $result->header = substr($response, 0, $header_size);
        $result->body = substr($response, $header_size);
        $result->http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        $result->last_url = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);

        curl_close($curl);
        return $result;
    }