I am new to CodeIgniter and Rest API. I am trying to implement REST API in CodeIgniter and have used Phil Sturgeon's rest-client and rest-server. I have watched few tutorials and have successfully implemented the Rest-Server part (checking with Chrome's Rest Client APP). But, to implement the Rest-Client, I am having few troubles.
Do I need to have cURL and CodeIgniter's cUrl Library?
If yes, how should I set it up?
I watched this tutorial too by Phil Sturgeon but in this tutorial, he has only used the Rest-Client function to call the Server. But not defined where to put it. Here's the code
function rest_client($id){
$this->load->library('rest', array(
'server' => 'http://localhost/rest/index.php/restgetcontroller/',
));
$user = $this->rest->get('user', array('id' => $id), 'json');
echo $user->name;
}
I am sorry if it is too simple.
Thank You
Edit: I made a Client controller and put there a method to call it. But when I load the page, I get this error.
Call to undefined method CI_Loader::spark()
You can use wherever you need to retrieve a value from your API.
$user will have a value you can use for your purpose.
Basically, you would use the API where you used to use a Model, because now the interaction with the database is made with the API, not from your Controllers directly.
To call RESTful APIs you'll require CURL, there's a library called Guzzlehttp to use CURL more efficiently.
You can use composer to install the library or simply download the zip and require it in your controller.
Example Usage:
try {
$guzzleHttp = new GuzzleHttp\Client([
'verify' => false
]);
$http_response = $guzzleHttp->request('GET', 'http://localhost/rest/index.php/restgetcontroller/');
$response = json_decode($http_response->getBody()->getContents());
return $data;
} catch (Exception $e) {
log_message('error', $e->getMessage());
return false;
}
Related
Im new to SOAP UI, so is there any method to extract specific values from SOAP UI and return in PHP i want to extract data from this Courier Service Tracking SOAP API and show it in PHP
SOAP API : http://webapp.tcscourier.com/codapi/Service1.asmx
SOAP Request : http://webapp.tcscourier.com/codapi/Service1.asmx?op=GetCNDetailsByReferenceNumber
Any help would be much appreciated.
Thanks in advance.
I think what you might want to do is:
First you init a connection to the remote soap-Server, therefore you need to initiate a client
$client = new SoapClient('http://webapp.tcscourier.com/codapi/Service1.asmx?WSDL');
Since its a public API you don't need Login Credentials
Then you want to call the remote Method, e.g. getAllCities and store the response in a variable;
$response = $client->getAllCities();
Now you can view the result:
var_dump($response);
To be sure nothing wents wrong you can put the code above in try...error
$client = new SoapClient('http://webapp.tcscourier.com/codapi/Service1.asmx?WSDL');
try {
$response = $client->getAllCities();
} catch (SoapFault $e) {
var_dump($e);
}
HTH - best regards,
Lupo
i am creating a web application in cakephp 2.9. it has two use,
To provide API access to android and Ios devices
To serve Web pages(Normal web applicaiton)
currently i am developing the api part , i create a webserviceController.php file and write all the Api , Now i feel like the controller is too fatty,
i dont know which is the best practice to manage API's. now my webservice controllerhave more than 2000 lines of code..
now iam planning to seperate the each api's in different controller
can anyone suggest me a good practice for writing apis in cakephp
currently my code look like
class WebServicesController extends Controller
{
public $uses = null;
public $components = array('RequestHandler', 'Verification', 'AppContstant','PushNotification');
public function users()
{
//code to get users
//
$this->set(array(
'result' => $result,
'_serialize' => 'result'
));
}
and in my route.php
Router::mapResources('WebServices');
Router::parseExtensions();
Router::resourceMap(array(
array('action' => 'users', 'method' => 'GET', 'id' => false),
i have more around 43 api's in webserviceController,
How to manage API's cakephp
What is the best practice?
_serialize vs json_encode() which is fast?
How to versioning my API url ?
The simplest way of making an API in cakephp is print the a JSON directly from the controller and stop the rendering by die().
Official document suggests you to have an API view for the output, which is a standard MVC way and is a good practice. You can check out more here: https://book.cakephp.org/2.0/en/views/json-and-xml-views.html
Maybe you can reference how Croogo implemented it, this is a Cakephp based CMS.
They make API as a component to manage version and methods: https://github.com/croogo/croogo/blob/master/Croogo/Controller/Component/BaseApiComponent.php
This is an example of its URL routing in format /$version/$model/$method.$format/?$parameters
http://www.example.com/croogo-2.2.2/api/v1.0/nodes/lookup.json?type=page&title=how
Sibin Francis, You can use rest api class and include in you api controller
add following line at top
App::import('Vendor', 'REST', array('file' => 'Rest.inc.php'));
Public function yourFunctionName() {
if ($this->request->is("post")) { // Use your method name put/post/get/etc
$rest = new REST();
// Your logic here
return $rest->response(json_encode($data), response_code);
}
}
Its very easy and simple. Only you need to call vendor Rest Api class.
Quick background: I'm fairly experienced with PHP, but needed to build my first RESTful API. I figured I'd try Laravel (5.2) and am starting to feel pretty comfortable with it.
I started adding auth to my project over the weekend and I am really struggling to get it working. I got the basic Laravel Auth middleware working quickly, but I think I need to be using OAuth2 for production (I will be building a mobile app that will connect up to this server). I'm using the Luca Degasperi OAuth2 package, which seems to be pretty popular.
I reviewed the actual documentation: https://github.com/lucadegasperi/oauth2-server-laravel/tree/master/docs#readme)
I also went through this tutorial: https://medium.com/#mshanak/laravel-5-token-based-authentication-ae258c12cfea#.5lszb67xb
And, most recently, I found this thread about the need to seed the OAuth tables before anything will work: https://github.com/lucadegasperi/oauth2-server-laravel/issues/56
That's all great, but there are some minor differences in the most recent distribution of Laravel. For example, /app/Http/Kernel.php is slightly different from what's shown in some of the examples I found because it now uses middleware groups. I thought I handled those differences correctly (I added the OAuthExceptionHandlerMiddleware class to the 'web' section of $middlewareGroups instead of $middleware). I got my seeder working (the current oauth_scopes table only allows you to supply a description, so I had to slim down what was provided in the third link above).
If I put a test route in my 'web' group in routes.php, I would have thought this would require OAuth because I added OAuth to the 'web' middleware group in Kernel.php. That's not the case. My route works with no authentication if I do that.
I then explicitly added the OAuth middleware to my test route as follows:
Route::get('tests/events', ['middleware' => 'oauth', function() {
$events = App\Event::get();
return response()->json($events);
}]);
That causes a 500 error ("ErrorException in OAuth2ServerServiceProvider.php line 126: explode() expects parameter 2 to be string, object given").
I'm to feel pretty lost. Each of these packages seems to be shifting so quickly that there's no complete documentation on how to get this up and running.
What else do I need to do to get this functioning?
The following link is what finally got me un-stuck:
https://github.com/lucadegasperi/oauth2-server-laravel/blob/master/docs/authorization-server/password.md
Now that I have it working, I'll try and make this a complete how-to FOR PASSWORD GRANT TYPES ONLY. I didn't play with other grant types. So this assumes you're building something like a RESTful API where users will connect to it with a client app that you're going to build. So users will create a user account in your system and then when they send a REST request, the OAuth2 package will authenticate them and send them a token to stay logged in.
I'm using Laravel 5.2 and already had the basic Auth package up and running. Be advised that a lot of these steps seem to change even with incremental releases of Laravel or the OAuth2 package.
The first part of getting this working is fairly well documented already (https://github.com/lucadegasperi/oauth2-server-laravel/tree/master/docs#readme), but here's a summary just in case...
Edit the require section of your composer.json file to look something like this:
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.2.*",
"lucadegasperi/oauth2-server-laravel": "5.1.*"
},
Run composer update to download the package.
Open your config/app.php file and add the following two lines to the end of the providers section:
LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider::class,
LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider::class,
Also in config/app.php, add this line to the aliases array:
'Authorizer' => LucaDegasperi\OAuth2Server\Facades\Authorizer::class,
Now we start to do things a little differently from the documentation to accommodate the current version of Laravel...
Open app/Http/Kernel.php. Laravel now uses groups and it didn't used to. Update your $middlewareGroups to look like this:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
//Added for OAuth2 Server
\LucaDegasperi\OAuth2Server\Middleware\OAuthExceptionHandlerMiddleware::class,
//Commented out for OAuth2 Server
//\App\Http\Middleware\VerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
],
];
Also in app/Http/kernel.php, update $routeMiddleware to look like this:
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
//Added for OAuth2 Server
'oauth' => \LucaDegasperi\OAuth2Server\Middleware\OAuthMiddleware::class,
'oauth-user' => \LucaDegasperi\OAuth2Server\Middleware\OAuthUserOwnerMiddleware::class,
'oauth-client' => \LucaDegasperi\OAuth2Server\Middleware\OAuthClientOwnerMiddleware::class,
'check-authorization-params' => \LucaDegasperi\OAuth2Server\Middleware\CheckAuthCodeRequestMiddleware::class,
'csrf' => App\Http\Middleware\VerifyCsrfToken::class,
];
You now have to set up your grant types. You used to do this all in one place in config\oauth2.php using an array with a closure for callback. With the most recent version of the OAuth2 server package, you can't use a closure for callback anymore. It has to be a string. So your grant_types should look something like this:
'grant_types' => [
'password' => [
'class' => '\League\OAuth2\Server\Grant\PasswordGrant',
'callback' => '\App\PasswordGrantVerifier#verify',
'access_token_ttl' => 3600
]
]
access_token_ttl is the duration that an auth token will be good for (in seconds). The main package documentation uses 3600 (1 hour) by default. You might want to try 604800 (1 week) instead -- at least during testing.
You now need to create the PasswordGrantVerifier class and verify method that you just called in the code section above. So you create a file App/PasswordGrantVerifier.php and use the following code (which is basically what used to go in the closure for callback).
<?php
namespace App;
use Illuminate\Support\Facades\Auth;
class PasswordGrantVerifier
{
public function verify($username, $password)
{
$credentials = [
'email' => $username,
'password' => $password,
];
if (Auth::once($credentials)) {
return Auth::user()->id;
}
return false;
}
}
You will need at least one row in the oauth_clients table before OAuth2 will work. You can insert something manually or create a seeder. To create a seeder, modify database/seeds/DatabaseSeeder.php and add the following to the end of the run() method:
$this->call(OAuthClientsTableSeeder::class);
Now create a file called database/seeds/OAuthClientsTableSeeder.php and enter something like this:
<?php
use Illuminate\Database\Seeder;
class OAuthClientsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
//Add sample users
$oAuthClients = array(
array(
'id' => 'TEST_ENVIRONMENT',
'secret' => 'b17b0ec30dbb6e1726a17972afad008be6a3e4a5',
'name' => 'TEST_ENVIRONMENT'
)
);
foreach ($oAuthClients as $oAuthClient) {
App\OAuthClient::create($oAuthClient);
}
}
}
Run php artisan vendor:publish to publish the package configuration and migrations. Run php artisan migrate to set up the billion-or-so new tables for OAuth. Run php artisan db:seed to seed your database.
You can now set up some test routes in app\Http\routes.php. They should look something like this:
Route::post('oauth/access_token', function() {
return Response::json(Authorizer::issueAccessToken());
});
Route::group(['middleware' => 'oauth'], function () {
Route::get('authroute', function() {
//OAuth will be required to access this route
});
Route::post('postwithauth', function(Request $request) {
$userID = Authorizer::getResourceOwnerId();
$input = $request->input();
return response()->json(array('userID' => $userID, 'input' => $input));
});
});
Route::get('noauthroute', function () {
//No authorization will be required to access this route
});
Pay close attention to the postwithauth route I included above. The OAuth2 package recently changed how you access the user's ID and it took me quite a while to figure out how to get it.
Now that it's time for testing, point your browser to localhost:8000 (or whatever the path is for your test environment) and create a user account for yourself (this step just uses the standard Laravel Auth package).
Go into your HTTP client (I'm currently using Paw and I like it). Go to request->authorization->OAuth2 to set up authorization for the route you're going to test. For Grant Type, select Resource Owner Password Credentials. If you used the seed example I provided above, the Client ID is TEST_ENVIRONMENT, the Client Secret is b17b0ec30dbb6e1726a17972afad008be6a3e4a5, enter the username (email) and password you created through the web Auth interface, your Access Toekn URL will be something like localhost:8000/oauth/access_token (depending on how you set up your test environment), leave Scope blank, and Token should say Bearer. Click on Get Access Token then say Use Access Token when prompted.
That should be it!
I can set a GET method in Slim to get data from my database but my problem is the POST method, i don't know how to use it correctly. I do some code like:
$app->post('/login',function() use ($app){
$inputs = json_decode($app->request()->getBody());
$result = json_encode($inputs);
return $result;
});
I wanna make a login function by POST method but this is just an example I want to show the data that have been sent in the body by json. I used Advanced Rest Client to test but the result is always "null".
I'm new to Rest and Slim Framework too. Thanks for any helpful idea !
using return doesn't do anything in terms of viewing the output within that route callback function. use print, print_r, echo, $app->response->setBody('Foo'), or $app->response->write('Foo')
in terms of the post, did you try using $data = $app->request()->post() to get your data?
I'm trying to get mail(imap) from google by oauth authorization . I have got the authorization to work, but I can not retrieve the emails. As I understand it should be posible. But Google does not have any api to retrieve mail(?). However, I found the following:
https://developers.google.com/google-apps/gmail/oauth_overview
That says:
Accessing mail using IMAP and sending mail using SMTP is often done
using existing IMAP and SMTP libraries for convenience. As long as
these libraries support the Simple Authentication and Security Layer
(SASL), they should be compatible with the OAuth mechanism supported
by Gmail. In addition to using a library which supports IMAP and SMTP,
developers also will want to use one of the many existing libraries
for handling OAuth
Do anyone know a existing library that i can use and that has some documentation as well. Im using google-api-php-client.
The code
session_start();
ini_set('display_errors',1);
error_reporting(-1);
require_once '../../src/apiClient.php';
$client = new apiClient();
$client->setApplicationName('Mailendar');
$client->setScopes("http://www.google.com/m8/feeds/");
// Documentation: http://code.google.com/apis/gdata/docs/2.0/basics.html
// Visit https://code.google.com/apis/console?api=contacts to generate your
// oauth2_client_id, oauth2_client_secret, and register your oauth2_redirect_uri.
$client->setClientId('secret');
$client->setClientSecret('secret');
$client->setRedirectUri('secret');
$client->setDeveloperKey('secret');
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
echo "token is set";
$client->setAccessToken($_SESSION['token']);
}
if (isset($_REQUEST['logout'])) {
unset($_SESSION['token']);
$client->revokeToken();
}
if ($client->getAccessToken()) {
MAGIC HAPPENS HERE!!!...but is unkown for me ofc
// The access token may have been updated lazily.
$_SESSION['token'] = $client->getAccessToken();
} else {
$auth = $client->createAuthUrl();
}
if (isset($auth)) {
print "<a class=login href='$auth'>Connect Me!</a>";
} else {
print "<a class=logout href='?logout'>Logout</a>";
}
Thanks!
Google doesnt allow you to retrieve mail with oauth 2.0 at the moment.
Now you can fetch mails using OAuth.
Implemented a simple library.
Delete mail function is not yet added. But you can take a look if it satisfies your need.
Try example. https://github.com/vmuthal/VivOAuthIMAP