Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slight changes to your library #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions tmhOAuth.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php
/**
* tmhOAuth
*
Expand All @@ -7,9 +7,9 @@
* REST requests. OAuth authentication is sent using the an Authorization Header.
*
* @author themattharris
* @version 0.1
* @version 0.1.1
*
* 26 August 2010
* 17 September 2010
*/
class tmhOAuth {
/**
Expand Down Expand Up @@ -174,8 +174,8 @@ private function prepare_url($url) {
$parts = parse_url($url);

$port = @$parts['port'];
$scheme = $parts['scheme'];
$host = $parts['host'];
$scheme = @$parts['scheme'];
$host = @$parts['host'];
$path = @$parts['path'];

$port or $port = ($scheme == 'https') ? '443' : '80';
Expand Down Expand Up @@ -315,15 +315,16 @@ private function sign($method, $url, $params, $useauth) {
* @param array $params the request parameters as an array of key=value pairs
* @param string $useauth whether to use authentication when making the request. Default true.
* @param string $multipart whether this request contains multipart data. Default false
* @param boolean $return_curl_handle whether the curl handle should be returned instead of executed. Default false
*/
function request($method, $url, $params=array(), $useauth=true, $multipart=false) {
function request($method, $url, $params=array(), $useauth=true, $multipart=false, $return_curl_handle=false) {
$this->config['multipart'] = $multipart;

$this->create_nonce();
$this->create_timestamp();

$this->sign($method, $url, $params, $useauth);
$this->curlit($multipart);
return $this->curlit($return_curl_handle);
}

/**
Expand Down Expand Up @@ -376,11 +377,11 @@ function auto_fix_time_request($method, $url, $params=array(), $useauth=true, $m
*/
function url($request, $format='json') {
$format = strlen($format) > 0 ? ".$format" : '';
return implode('/', array(
return implode('/', array_filter(array(
$this->config['host'],
$this->config['v'],
$request . $format
));
)));
}

/**
Expand All @@ -391,7 +392,7 @@ function url($request, $format='json') {
* @param string $header the response headers
* @return the string length of the header
*/
private function curlHeader($ch, $header) {
function curlHeader($ch, $header) {
$i = strpos($header, ':');
if ( ! empty($i) ) {
$key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
Expand All @@ -402,12 +403,11 @@ private function curlHeader($ch, $header) {
}

/**
* Makes a curl request. Takes no parameters as all should have been prepared
* by the request method
*
* @return void response data is stored in the class variable 'response'
* Makes a curl request. All should have been prepared by the request method
* @param $return_curl_handle boolean Defaults to false
* @return When $return_curl_handle is TRUE, returns the curl handle; otherwise void: response data is stored in the class variable 'response'
*/
private function curlit() {
private function curlit($return_curl_handle=false) {
if (@$this->config['prevent_request'])
return;

Expand Down Expand Up @@ -472,6 +472,11 @@ private function curlit() {
curl_setopt($c, CURLOPT_HTTPHEADER, $this->headers);
}

// return the curl handle?
if ($return_curl_handle) {
return $c;
}

// do it!
$response = curl_exec($c);
$code = curl_getinfo($c, CURLINFO_HTTP_CODE);
Expand Down