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
Related
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.
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.
I know this question already has been discussed, But I'm here with a different way and a confusion.
First : if I do debugging in Graph API Explorer it returns different app_scoped_user_id and it return me correct json when I run graph.facebook.com/$app_scoped_user_id but with my app it generate different App Scoped ID which don't works with above method. Why that happen?
Second : I thought to get last redirect url using CURL but it returns http://www.facebook.com/login.php?next=http://www.facebook.com/app_scoped_user_id/$app_scoped_user_id . Now I've two thoughts (may be stupid thoughts),
Can I open that app scoped url in a popup window and get that window url after redirection and close the popup?
Can I login in Facebook anyhow using CURL so that i can get real user_id?
I know, app scoped user id can be used. But I want to give it a try.
Warning: Facebook old API in use
There's a way to get it, but you won't like it:
get the user to log in to your app via Facebook
make a second call to the API using the retrieved access token to get their username
https://graph.facebook.com/me?access_token={THE_ACCESS_TOKEN}
make a third call to that username without an access token to get their Facebook id
https://graph.facebook/{USERNAME} <= No access token here!
EDIT: sorry, old API is used here.
I need them to import their images into my site.
In their documentation they authenticate a use with a response code by the following url
https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code
The above URL gives me only a code not all the images. And this is not my target. When a user come to my site and click on the Instagram login button with the user name and password then I need to import all the images of that user into my site.
Is that possible?
Follow this until you got yourself an Access Token: http://instagram.com/developer/authentication/
Once you got that, you can call https://api.instagram.com/v1/users/self/media/recent?access_token=YOURACCESSTOKEN
This will return you a JSON-Result of the newest Images of the User. Inside the pagination element, you will find a URL, that you can call to request the next Set of Images.
For more info on the parameters have a look at http://instagram.com/developer/endpoints/users/
A great way to get familiar with the API is to play around with the API console: http://instagram.com/developer/api-console/
Research and Reasoning
I haven't worked on a Facebook Application for a while, thus am a little rusty with how the Graph API works.
I have had a look at various topics and pages such as:
https://developers.facebook.com/docs/reference/plugins/activity/
How to Style Facebook Activity Feed
From what I can gather, getting the activity of a facebook page is pretty simple, however, before I proceed, there are a few things I would like to iron out before to ensure I build the app or write the script in the best possible way...
My Question
I would like to gather the status updates of a Facebook Like page and then feed this data to my News page on the website I am building.
My method would be a simple call to the Graph API to gather the data, and then display it on screen.
My question is, can I do this without creating a Facebook App, and therefore do I even need the PHP SDK?
Is it possible to achieve it like so and are there any request limits:
$url = 'https://graph.facebook.com/LIKE_PAGE_ID/feed';
$result = json_decode(file_get_contents($url));
If the above is the correct answer to this question then please let me know and I will close this question
Update
I have tried the above URL with my like page and it says that I need an Access Token. This is what I feared... Is it possible to get this data without being logged in, and without having set up a Facebook Application?
Why not create a Facebook app and use App Access Token to retrieve the feed for the Public pages.
Though you might get feed of some pages without app access token or response for different end points without access_token, but as the Facebook's API is constantly changing, and would finally settle with authorized request to their end points, I would suggest you to use the App Access Token which is can be created with the format
App_ID|App_Secret
So instead of your call for $url that you had you can change it to
$url = 'https://graph.facebook.com/LIKE_PAGE_ID/feed?access_token=App_ID|App_Secret';
Which you can then json_decode. Also there is nothing on binding to use PHP SDK, and in your case, it is not even required.