How to allow other site retrieve data from your site? - php

Details
I'm new to RESTful API & Laravel world.
But "Every artist was first an amateur. - right ? "
Let's get to the point.
I have 2 sites. Let's call it :
A
B
Website B have a a nice list if users table + all of its relation.
I want to allow website A to access website B and load those nice data.
I never done this. I assume that the logics are :
Website A will need some kind of api_keys to access into website B.
Then after the api_key match, website B will return the data back to website A as json file.
Then, website A will receive that json file, and load them into HTML and display it.
Is my logic is even close ? Please - correct me if I am wrong. :)
Here is what I've tried
After doing some researches, I came across this site. I really liked it. I finished it all way.
Now, I kind of get a sense of RESTful API a little more.
Then, I came across this site. I found this
filters.php
Route::filter('api', function() {
// Fetch a user record based on api key
$user = User::where('api_key', '=', Input::get('api_key'))
->take(1)
->get();
if ($user->count() > 0) {
Auth::onceUsingId($user[0]->id); // Authorize the user for this one request
} else {
return Response::view('errors.404', array(), 404)->header('Content-Type', 'application/json');
}
});
I notice the OP of this, stored api_key in users table.
My questions
Do I need to do that like him ?
Is this the only way to do it ?
Is there a better/easier way to do this ?
Rather than grab the api_key from the database, Can I just manually set it to a random number + text like this '21sdf364rt7y6r5ty1u28x1h8gt7yt2ert3654871' ?
How long will the api_key be expire ? Is it even expire ? How do we know that ?
Again, my main goal is to allow website A to access the stuffs from website B.

Is my logic is even close ?
Yes it close enough.
So, the main goal is to access the stuffs from website B. You decide whether stuff from B is restricted content or not.
If users that can access the stuff is limited, yes you need an api keys.
If content for site B is for public, you can just print json data without any required api keys like github does: https://api.github.com/users/github.

Related

Increment Number When request GET like session using JWT lumen

I'm Create a API with Lumen Using JWT, then the request set to GET this method show the detail about the data like reading page
I try to using Session Like Laravel but it's not working, and i know Lumen not using session + i using JWT token for authentication
and this my code
public function show($storyId, $id)
{
$part = Story::find($id)->parts()->withCount('comments')->first();
$link = 'story/'. $storyId.'/part/'.$id;
if (!Session::has($link)) {
$part->increment('viewers');
Session::put($link,1);
}
return response()->json($part);
}
so every request method GET call it increment the viewers during the token not expired, or like youtube viewers as long as browser not closed
if i wrong to write a code or implement the code please suggest me for effective code
I think the best approach to implement video views on your web app:
create table with columns ('video_link',address_'ip') and every view store the video link with the viewer ip , so in the next of some visitor view you check if the current link video and vistor ip has exists on views table, if doesn't exist incrment video views count else nothing to do

Need Help About Laravel Api

I have two web Application that's work in different domaine , The first one is a Laravel web application like this
domaine1.com
and another web application that's built also with laravel
domaine2.com
and i have on the first application a dashboard like this ( domaine1.com/dashboard) ,
I want to add from this dashboard for example A books to the the web Application 2 , that's have it own database and tables ......
Please can someone tell me how can i do it ?
i'm kind of Newbie on laravel , i think i should Use something like API ? or Something else ?
Create a route in your Application 2 routes.php to get the books
for example
Route::get('domaine2.com/books', 'BookController#getBooks');
In your BookController:
public function getBooks()
{
$books = Books::all(); //I assume that Books is your model
return $books
}
Now all you have to do is call this in a function in your controller from domaine1.com application to have your books
$books = file_get_contents('http://domaine2.com/books');
To give it a try first and be sure it works try accessing http://domaine2.com/books to see if you get a json with your books.
Yes. You should use API. Although it will not be that easy, because you want to create cross-domain requests.
If data you want to pass is not classified, you can make a public API handler, meaning that everyone will be allowed to access this. This can be for example a get request that return some objects from your database. Let's say you create a route in your domain1.com:
GET domain1.com/api/books that returns json/data
Then if anyone visits http://domain1.com/api/books he will see this response formatted in json. You can utilize it in your domain2.com app using CURL or built in vue.js with axios.
If data you want to provide is classified or you want to make requests other than GET (POST for example) you will have to read about application authorization. Putting in a simple way: you will have to show your domain1.com app that someone who wants to access restricted data is allowed to do it.
By the way, mentioning other response: you should utilize api.php, not web.php in routes. And have in mind that file_get_contents is significantly slower than CURL.

Log out user from Android app when user signs in on website

I have an Android app and a website that offer the same content to a user. I'm trying to accomplish the following:
If a user signs in on the website the Android app should sign him out automatically and if the user signs into their account from the Android app then the website should sign them out. This is to make sure at one time the user can access either the web app or the Android app, not together.
I'm looking for best practices and solutions to accomplish this.
Here's what I have come up with:
When the user logs in either from Android or website I update a field in the database called "login_device" this will be either "web" or "mobile" depending on the situation.
If "login_device" is "mobile" and the user logs in from the website it will change to "web" and the Android app will log out the user.
If "login_device" is "web" and the user logs in from the Android app, this value changes to "mobile" and the user is logged out from the web.
Also, I have created a web service called "ping" for the Android app.
But, in this approach there would be a need for the Android app to maintain a constant connection with the server to check the value of "login_device".
Here's the ping service code:
<?php
require_once 'transact/info.php';
require_once 'transact/database.php';
require_once 'transact/func.php';
$data = file_get_contents('php://input');
$json = json_decode($data);
$id = mysql_real_escape_string($json->{'user_id'});
$query = 'select login_device from users where id = '.$id;
$result=mysql_query($query) or die('error getting admin details : '.mysql_error());
$row = mysql_fetch_array($result);
$array = array("login_device" => $row['login_device']);
print( json_encode($array));
?>
So, my questions are:
Q - In this approach how can I maintain a constant connection with the sever to check the value of "login_device:
Q - Is there a better and more standard way of trying to accomplish the above?
Thanks for helping out!
Q - In this approach how can I maintain a constant connection with the sever to check the value of "login_device:
Define a standard time interval in which even if someone logins to the website, they dont really have enough time to break any rules (according to what your apps are doing) (for example 10 seconds), and ping the website in this interval. The architecture of the mobile apps is usually based on REST technologies. That said, there is no really permanent connection. The closer you can get is very close together pings. So, you only need to define "how often is often enough for me"
Q - Is there a better and more standard way of trying to accomplish the above?
No, because this is not a usual requirement. Most applications/websites right now do not disallow permanent usage from multiple devices. So how you have started doing it seems like the best solution
Have you considered building a website using node.js and then turning it into a mobile app by wrapping it in Phone Gap and XCode to turn it into Droid and iOS apps respectively. Node.js will keep a synchronous connection open to your app, which will allow push events when the login status of the user changes.

User Statistics in php

I am using Codeigniter for my current project.
The peoject is looks like a social platform,so i want to display users data on user admin panel.
I have the following types of domains for users.
http://site.com/page_name/
so page_name is a page of a particular user,so when that user logged in to his control panel i want to show the usage statistics of page_name to him.
On Cpanel we can see so many LOG programs ,is there any plugin available to integrate this on codeigniter/php ?
i heared about Awstats ,can i use it on my case ? or how they saving the data on the server,if they are using a database then i can easily get the data from datbase.
or any other methods ? custom things ?
All suggestions and opinions are welcome.
Thank you.
Update : I want statistics for every pages on my project
they are not in subdomains its looks like site.com/page_name .
If i can use Google Analatycs API ,then how can i track these pages ??
You could use Piwik and inserting the link into a view.
Piwik is like google analystics, but on your server. Thats what i like, nobody else can get access to your stats.
Building custom analytics isn't hard (unless you want graphs, but this is fairly easy to do with jqplot - http://www.jqplot.com/).
You could just autoload a library that would populate a db table (through a model) with current_url() or uri_string(), and then pull the stats out for each user.

Getting Twitter friends to allow user to tag them in post

Im using the following Twitter API calls to get the list of people a user follows:
https://dev.twitter.com/docs/api/1/get/users/lookup
https://dev.twitter.com/docs/api/1/get/friends/ids
The idea is to allow the user to send DMs to their friends. Now I have a performance issue ahead of me and I want to see how others have solved it. I have 2 options:
1) Whenever the user logs on, if they have a Twitter account I query the friend list, index alphabetically in an array for easier access and store it in the user's sessions. The problem with his is that it's done every time the user logs on, so if he's following a lot of people it's gonna take a long time, since it can only lookup 100 users at a time, and there's also the rate limit deal. The advantage is it will always have the user list up to date.
2) Do the same when the user associates his Twitter account with my site, and store it in the database, this will make it easier when I have to search the friend list since I can just execute a SELECT query using LIKE and get a matching list to whatever the user is typing, I'm just not sure if this is wise because it would take up a lot of space.
I'm using PHP with CakePHP framework and JQuery. Any other ideas are welcome.
UPDATE:
I decided to go with the DB approach, it takes very lone to query for all the friends so I'd rather do it as little as possible. Now, how would you approach updating this list?
Instead of retrieving the users you could also retrieve the id's only and search the extra information of the users when needed.
Here's an example in jQuery
$.getJSON('http://api.twitter.com/1/friends/ids.json?screen_name=smashingmag&callback=?', function(data) {
$.each(data.ids, function(idx, item) {
console.log('userId: ' + item);
});
});
For more information on the method see: https://dev.twitter.com/docs/api/1/get/friends/ids

Categories