Php REST webservice: are those url are logical or physical? - php

Hi I'm trying to build the server side of an application using REST archtecture with PHP. later I will make a client for android to consume those services.
I already know what is a RESTful application.
but still got something confuse:
if I want to get retrieve information of a user, the url I should access is www.domain.com/user/123 according to REST. but that means for every user in DB I should make a .php?? that sounds illogical. or there are something like java which use logical url, or just rewrite the url to something like www.domain.com/getusr.php?id=123 ??
fixed: is url rewrite the correct way to interpret request url from clients for a webservice in php?
for example: if someone request www.domain.com/user/123, I should rewrite to www.domain.com/getusr.php?id=123
or maybe php is not for webservice?
thanks

You need to rewrite the url to www.domain.com/getusr.php?id=123 , that way you need only 1 php file , all requests in form of www.domain.com/user/123 would go to a single getusr.php file . You can achive this using apache mod_rewrite ( there are tons of examples around so i whont post another one ) .
Edit
Check this link , that contains a few good examples .
Or you can use this code bellow ( place it inside a ".htaccess" file at the root of you're web directory , where getusr.php should be placed allso )
RewriteEngine on
RewriteRule ^user/([0-9]+)$ getusr.php?id=$1

I like the way Drupal does it. Drupal uses a q parameter for all of its routes:
http://myserver/drupal/?q=node/234
Which, if mod_rewrite is enabled, becomes
http://myserver/drupal/node/234
This way, you just have to parse the contents of $_GET['q'].
It's cleaner than having a bunch of separate rewrite rules for every one of your routes.

Related

Why long URL's and what are those extra characters for?

I was looking at a few web e-commerce website and noticed that they usually have long URL something like this (taken from an e-commerce site)
https://www.example.com/bsquare-tuxedo-solid-men-s-suit/p/itmejr4zbjt6fr3h?gclid=CjwKEAiAz4XFBRCW87vj6-28uFMSJAAHeGZbXBpy4Rw5UckmBAiPaacHLhr5MbPj4bMxVThaQe5A3xoCIi7w_wcB&pid=SUIEJR4ZAK7FZKRT&cmpid=content_suit_8965229628_gmc_pla&tgi=sem%2C1%2CG%2C11214002%2Cg%2Csearch%2C%2C95089233620%2C1o5%2C%2C%2Cc%2C%2C%2C%2C%2C%2C%2C&s_kwcid=AL!739!3!95089233620!!!g!182171694500!&ef_id=WJitiQAAAfz4lw1Y%3A20170213091758%3As
Generally a router would be
http://www.example.com/controller/method/param1/param2
Then what all codes go up in the longer URL as above and why do a longer version when a short URL can get the same job done?
You can start to read more about get method:
http://www.html-form-guide.com/php-form/php-form-get.html
To send data server side in php you will need to make a request get or post. The second link is an get but rewritten.In the end to access your data the url will be rewritten by the framework you use ( http://www.example.com/controller/method?id1=param1&id2=param2 ). If you do not use any framework you will need to rewrite yourself with .htaccess Usually the second link is used for
more common pages so will be better indexed by google. The first link is for data that is more dynamic and may have more or less parameters, null or "" values etc. And also you do not care to index that page. For exempla a search : http://www.example.com/controller/method?searchValue=&page=1
You also can read more here. It explain how and why we rewrite links: https://www.smashingmagazine.com/2011/11/introduction-to-url-rewriting/

PHP Get method without?

Hey so im working on a website and one part of it allows you to lookup a user based on their name. At the moment i have it using a $_GET request so the link would look like:
http://website.com/p?name=John+Smith
How would i be able to remove that ?name= because i see alot of sites doing things like:
http://website.com/p/John+Smith
how would i achieve this because to my knowladge their arent any other forum request types only Post and Get?
URL rewriting is definitely what you're looking to do. It's well worth playing carefully with it but lots of testing is recommended. With great power comes great responsibility!
Most dynamic sites include variables in their URLs that tell the site what information to show the user. The example you provided is exactly like this.
Unfortunately, a cleaned up URL cannot be easily understood by a server without some work. When a request is made for the clean URL, the server needs to work out how to process it so that it knows what to send back to the user. URL rewriting is the technique used to "translate" a URL like the last one into something the server can understand.
To accomplish this, you need to first create a text document called ".htaccess" to contain the rules. This would be placed in the root directory of the server. To tell the server to rewrite a URL pattern, you need to add the following to the file:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^p/[A-Za-z\+]$ /p/?name=$1 [NC,L] # Rewriting rule here
The NC bit denotes case insensitive URLs and the L indicates this is the last rule that should be applied before attempting to access the final URL.
You can do quite a bit with this one rule, but the specifics extend far beyond the space of my answer here.
https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
I would highly suggest reading that thorough guide to help you on your quest!

Is it possible to REWRITE a URL without htaccess file?

Is it possible to shorten a url without using htaccess file?
for example I have this url.
this/is/a/very/long/url.php
change to
short/url.php
I hope I can get good answer THX guys:)
yes, mostly used in frameworks
an approach called using a front controller
ex: your front controller is index.php
your page links are generated as a fashion of .../index.php/nything/url.php
but the actual link is .../this/is/long/url.php
the front controller extract the page information the client requested and show the relevant page related to it
read more : http://en.wikipedia.org/wiki/Front_Controller_pattern
This is a good question, there are multiple options however the .htaccess file is probably your best bet.
this SO post describes it
Handling the url rewriting serverside is key here since it will be much faster to execute and will not break your script when used on some URL's.
so www.yourdomain.com/test/4/twenty/long/url/could/be/shorter/
all the arguments after www.yourdomain.com, can be retrieved via various PHP methods, including reading up on PHP's $_SERVER would be a good idea, as lots of variables are placed in that global array.
mod_rewrite is part of Apache, so it needs to be configured in Apache config. .htaccess actually Apache "live" config "per directory".
In general the best way ( I think) to rewrite urls is to rewrite everything to one file, let's say index.php and then "redirect" to specific file basing on URL. You can read a lot about "URL routing" in PHP on the web.
You can use this code in Javascript for rewriting current URL:
if (location.href.indexOf("this/is/a/very/long/url.php") > -1)
location.assign(location.href.replace(/this\/is\/a\/very\/long\/(url\.php)/, "short/$1"));

Getting value from database to a folder name PHP&MYSQL

I am having a problem with a PHP script, so the link is like this: page.php?id=1412.
But I don't want to get the data using $_GET['id']; is there a way for the program to recognize the URL like this localhost/1412, and then try to find the 1412 in the id field in database?
If possible how can I avoid the Not Found page ? since the folder 1412 does not exist ?
For all of this kind of matching you need to use either .htaccess files for apache, or similar functionality implemented in the configuration files of nginx/lighthttpd and so on. Basicly, when a request like x/1412 will come, you can match it before being dispatched to the php engine and transform it internally into x?id=1412 . Other solution involves redirecting all requests to a front controller (a script that is serving as a single entry point into the application) and let a url matcher match the route to execute. The latter is implemented in about every big/small php framework.
You could regex the current uri ($_SERVER['REQUEST_URI']). See for creating regex: PHP URL Regex and Parameter
.htaccess file is a solution, you can access this type of url and can send the value to php files using .htaccess file refer this nice article for doing this
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/

A Dynamic website using PHP

I am a beginner PHP programmer. I searched google for a "Dynamic PHP website tutorials". I found some stuff. They use $_GET variable to make the website dynamic, so the URL's appear like this:
example.com/?page=home
example.com/?page=about
example.com/?page=Downloads
and so on...
But most of the dynamic websites that I found on the internet has links like this:
example.com
example.com/about
example.com/download and so on....
So how do they do so ?? Have they got folders for all the catogories ?? And Also some websites have article URLs (eg : example.com/articles/posts/2010/article1.php). It would be a reall mess if they've got folders for all items. If not then How ?? Can someone give an example please ?
If you're using apache then read: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
If you're using IIS then read: http://www.iis.net/downloads/microsoft/url-rewrite
In order to use the $_GET variable, it must be in the query string (or being routed through some other means that isn't 'default').
For example, the URLs you're using would become.
example.com/?page=home
example.com/?page=about
example.com/?page=Downloads
Additionally, you can rewrite URLs using the .htaccess file (http://httpd.apache.org/docs/2.0/misc/rewriteguide.html)
You are interested in page routing.
htaccess and MVC routing may start you down the correct path :)
To echo everyone else, it's called a url rewite.
For example, the url
http://example.com/index.php?ext=blog&cat=news&date=12122012
can be rewritten as
http://example.com/blog/news/12-12-2012
This isn't automatic, it requires defining the patterns used for understanding the new URL in a file called .htaccess which usually resides in the servers root directory. Note that the preceding '.' in the filename makes it a hidden file.
When I was first getting used to PHP i found the site http://phpbuilder.com a great help. They have a lot of articles, and a forum that is fairly nice to noobies. http://devshed.com is a good site too, and has a large amount of information on subjects outside of PHP.
You can achieve that affect with folders, but most use rewrites (Apache). It's a bit too broad of a subject to go in to here, but if you just search for rewrite tutorials you'll find some pretty quickly.
The $_GET is only to get variables from the URL. While this can be used to make sites dynamic, this is a technique which is usually frowned upon.
With rewrites, you basically have a URL like /about, but the rewrite tells your server something like "act like this is actually ?page="about"), which you then use the $_GET to process.
Being PHP beginner I will not urge you to use .htaccess, As you will need to learn lot many things before you proceed further. You have 2 option to send a request one is GET and POST. You can get more information about same on internet.
Also you have an option to start your dynamic website using CMS and I will recommend you to use wordpress. CMS will have some in-built function which will help you to do your work faster. Also using their control panel you can update the URL format.
I will also urge you to go step by step and follow every tutorial that you will find on internet.
All the best
If you want to do this you have to use .htaccess file and have to load mod_rewrite in your apache server.
In you root directory create file named .htaccess
Then Write:
RewriteEngine On
RewriteRule ^(.*)\.php$ index.php?page=$1 [L,QSA]
And After that call a page
my-page.php
It will redirected as a index.php?page=my-page internally but in browser it will show as my-page.php

Categories