Get requested service in Restler iAuthenticate implementation - php

I'm using Restler to develop a REST api and I need to get the requested service from the iAuthenticate implementation.
So far I have managed to get here:
$m = preg_match('/.+?\/(?P<api>.+?)\/(?P<service>\w+)/', $_SERVER['REQUEST_URI']);
and $_SERVER['REQUEST_URI'] has this form: /somedir/apiclass/requestedservice?...
I've tried my regex here: http://www.spaweditor.com/scripts/regex/index.php with my actual uri
and it works perfectly. When I try to parse the request url inside my iAuthenticate implementation it just don't work.
Does anybody know how to enable regex within restler iAuthenticate implementation? How can I display errors in Restler instead of a blank page?
Thanks!
[EDIT]
I wasn't passing the variable to store the match object, preg_match recieves a third argument to store it and just returns a boolean. Case closed.

I finally solved it like this:
preg_match('/.+?\/(?P<api>.+?)\/(?P<service>\w+)/', $_SERVER['REQUEST_URI'], $mo);
$api = $mo['api'];
$service = $mo['service'];

Related

How do you get the HTTP host with Laravel 5

I'm trying to get the hostname from an HTTP request using Laravel 5, including the subdomain (e.g., dev.site.com). I can't find anything about this in the docs, but I would think that should be pretty simple. Anyone know how to do this?
Good news! It turns out this is actually pretty easy, although Laravel's Request documentation is a bit lacking (the method I wanted is inherited from Symfony's Request class). If you're in a controller method, you can inject the request object, which has a getHttpHost method. This provides exactly what I was looking for:
public function anyMyRoute(Request $request) {
$host = $request->getHttpHost(); // returns dev.site.com
}
From anywhere else in your code, you can still access the request object using the request helper function, so this would look like:
$host = request()->getHttpHost(); // returns dev.site.com
If you want to include the http/https part of the URL, you can just use the getSchemeAndHttpHost method instead:
$host = $request->getSchemeAndHttpHost(); // returns https://dev.site.com
There two ways, so be careful:
<?php
$host = request()->getHttpHost(); // With port if there is. Eg: mydomain.com:81
$host = request()->getHost(); // Only hostname Eg: mydomain.com
laravel 5.6 and above
request()->getSchemeAndHttpHost()
Example of use in blade :
{{ request()->getSchemeAndHttpHost() }}
You can use request()->url();
Also you can dump the complete request()->headers();
And see if that data is useful for you.

delete with Zend_Rest_Client

I'm trying to delete a resource using ZF1 rest client
$this->restClient = new Zend_Rest_Client('https://myurl.com');
$response = $this->restClient->delete('/service/'.$this->uuid.'.json?api_key='.$this->apikey);
but I get an error:
Path "/service/v-2149d050-c64b-0131-33b0-1231390c0c78.json?api_key=a-9a136a00-b340-0131-2662-1231390c0c78" is not a valid HTTP path
the web service documentation simply says to use
DELETE https://myurl.com/service/YOUR_UUID.json?api_key=YOUR_API_KEY
any idea on how to use this class?
thanks
DELETE https://myurl.com/service/YOUR_UUID.json?api_key=YOUR_API_KEY
That is not the path only, but a full URI. It breaks down to:
Path: service/YOUR_UUID.json
Query-Info: api_key=YOUR_API_KEY
For Zend rest client, you need to call one function per each parameter, and a parameter can not be named as a standard HTTP verb:
$client = new Zend_Rest_Client('https://exeample.com');
$client->api_key(YOUR_API_KEY);
$response = $client->restClient->delete('/service/'.$this->uuid.'.json);
For more information please see the Request Arguments section in the vendor documentation on how to pass arguments with your request.

Instantiate a \Zend\Stdlib\RequestInterface

I want to use the method match(\Zend\Stdlib\RequestInterface) of Zend\Mvc\Router\Http\TreeRouteStack.
But I don't find any way to get the request I'm searching for.
This articles says how to get a request object outside a controler. I'm searching for an equivalent with ZF2
Finally I found how to fix by browsing the unit testing of the zf2
https://github.com/zendframework/zf2/blob/master/tests/ZendTest/Mvc/Router/Http/TreeRouteStackTest.php
The Zend\Http\PhpEnvironment\Request; will give a valid Request for the match method
use Zend\Http\PhpEnvironment\Request as PhpRequest;
/*TreeRouteStack*/
$request = new PhpRequest();
$stack->match($request);

Company Search on LinkedIn API with PHP?

This is making me crazy. I've been trying out all sorts of things but I just can't figure this out. LinkedIn's documentation is horrible...
All I need to do is simple: I need to search for a company (using a keyword) and retrieve the company id. I have issues with setting up the OAuth request and with making the request. Any advice on how to do this, especially without installing any PHP libraries?
FYI, my code: I got the OAuth.php from here.
require_once 'OAuth.php';
$base_url = 'http://api.linkedin.com/v1/company-search';
$consumer = new OAuthConsumer('mykey', 'mysecret');
$token = new OAuthToken('tokenkey', 'tokensecret');
$parameters = array (keyword => 'Apple');
$request = OAuthRequest::from_consumer_and_token($consumer, $token, "GET", $base_url, $parameters);
print_r($request);
Thanks
You're using a good OAuth library that I do recommend, but it seems to me you're missing on the Linkedin Library, or at least you haven't included the full code.
In any case, try using the simple-linkedinphp library, which uses your oauth library above. I had to use a few libraries in the past, and this is one of the best, particuarly if you're going to use Faceted search. Make sure you also check the quick start guide, it will help you a lot, as well as the class reference. The company search API function could be found here.
You could always use the raw() function for any custom calls not supported by the above function. I had to use that for some calls.

CodeIgniter Twitter API (Elliot Haughin's) capturing any URL get parameters with "oauth_token" in it

Seems a little strange.
I am using CodeIgniter with Elliot Haughin's Twitter library. It's an excellent API by the way.
However, I autoload this library in "autoload.php" in the config folder and I noticed ANY URL that has "oauth_token" URL parameter is captured by this library.
For example, when I type
http://localhost/myapp/index.php/controller?oauth_token=1
Then it throws up an error
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: libraries/Tweet.php
Line Number: 205
I went through the library and found that the following constructor calling a method that checks the GET parameters.
class tweetOauth extends tweetConnection {
function __construct()
{
parent::__construct();
..
..
..
$this->_checkLogin();
}
and the method "_checkLogin()" does the following
private function _checkLogin()
{
if ( isset($_GET['oauth_token']) )
{
$this->_setAccessKey($_GET['oauth_token']);
$token = $this->_getAccessToken();
$token = $token->_result;
...
...
What would be the best way to fix this?
Why do you have oauth_token in the querystring if you're not checking for a valid oauth_token?
I'm assuming at some point you want to state that a oauth_token has been set and you're just using oauth_token=1 as the parameter?
The library is set to always test against oauth_token, and it's not really a feasible tweak to do it any other way if you're autoloading it. You'd need a whitelist/blacklist of controllers (and maybe methods) it runs on, which pretty much defeats the point of autoloading.
If REALLY need to use oauth_token=1, you could just change it to
if ( isset($_GET['oauth_token']) && $_GET['oauth_token']!==1)
If you were using more than one value for oauth_token (or if your worry is that you can trigger an error by appending oauth_token=X to a URL) you could try and use a regex instead, assuming that all oauth_tokens follow a pattern (32 characters long etc).
Alternatively you could also probably just exit/return false depending on what is returned in $token = $this->_getAccessToken(). Depends what happens elsewhere in the code. Looks like returning false should just work.

Categories