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

Updated package to use Guzzle 5 for Laravel4. #21

Open
wants to merge 2 commits into
base: v1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"require": {
"php": ">=5.3.0",
"guzzle/guzzle": "3.9.1"
"guzzlehttp/guzzle": "5.3.*@dev"
},
"require-dev": {
"illuminate/support": "4.1.x"
Expand Down
2 changes: 1 addition & 1 deletion src/Teepluss/Hmvc/Commands/HmvcCallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function fire()
parse_str($parameters, $parameters);
}

// User credentails.
// User credentials.
$auth = $this->option('user');

if ($auth)
Expand Down
50 changes: 32 additions & 18 deletions src/Teepluss/Hmvc/Hmvc.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace Teepluss\Hmvc;

use Guzzle\Http\Client;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Routing\Router;
Expand Down Expand Up @@ -35,7 +35,7 @@ class Hmvc {
/**
* Remote client.
*
* @var \Guzzle\Http\Client
* @var \GuzzleHttp\Client
*/
protected $remoteClient;

Expand Down Expand Up @@ -141,7 +141,7 @@ public function invoke($uri, $method, $parameters = array())
/**
* Configure remote client for http request.
*
* @param array $configuration
* @param array $configurations
*
* array
*(
Expand All @@ -150,6 +150,8 @@ public function invoke($uri, $method, $parameters = array())
* 'headers/X-Foo', 'Bar', //custom header
* 'auth', array('username', 'password', 'Digest'), //custom authentication
*)
*
* @return $this
*/
public function configureRemoteClient($configurations)
{
Expand All @@ -158,9 +160,6 @@ public function configureRemoteClient($configurations)
call_user_func_array(array($this->remoteClient, 'setDefaultOption'), array($option, $value));
}


//sd($this->remoteClient);

return $this;
}

Expand All @@ -169,28 +168,42 @@ public function configureRemoteClient($configurations)
*
* @param string $uri
* @param string $method
* @param array $parameters
* @param array $parameters = [[data], [options]]
* @return mixed
*/
public function invokeRemote($uri, $method = 'GET', $parameters = array())
{
$remoteClient = $this->getRemoteClient();

// Make request.
$request = call_user_func_array(array($remoteClient, $method), array($uri, null, $parameters));
//to maintain compatibility, data is the first parameter value
list($data, $options) = array_merge($parameters, [], []);
if (!empty($data)) {
if (($method == "GET") && !isset($options['query'])) {
$options['query'] = $data;
} elseif (!isset($options['body']) && !isset($options['json'])) {
$options['body'] = $data;
}
}
if (!isset($options['verify'])) {
$options['verify'] = false;
}

// Ignore all SSL case.
$request->getCurlOptions()->set(CURLOPT_SSL_VERIFYHOST, false);
$request->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, false);

// Make request.
$request = $remoteClient->createRequest($method, $uri, $options);

// Send request.
$response = $request->send();
$response = $remoteClient->send($request);

// Body responsed.
if (isset($options['future']) && $options['future'] == true) {
return $response;
}

// Body response.
$body = (string) $response->getBody();

// Decode json content.
if ($response->getContentType() == 'application/json')
if ($response->getHeader('Content-Type') == 'application/json')
{
if (function_exists('json_decode') and is_string($body))
{
Expand All @@ -212,14 +225,15 @@ public function __call($method, $parameters = array())
{
$uri = array_shift($parameters);

$parameters = current($parameters);
$parameters = is_array($parameters) ? $parameters : array();

if (preg_match('/^http(s)?/', $uri))
{
$method = strtoupper($method);
return $this->invokeRemote($uri, $method, $parameters);
}

$parameters = current($parameters);
$parameters = is_array($parameters) ? $parameters : array();

return $this->invoke($uri, $method, $parameters);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Teepluss/Hmvc/HmvcServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace Teepluss\Hmvc;

use Guzzle\Http\Client;
use GuzzleHttp\Client;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\ServiceProvider;

Expand Down