{"id":341,"date":"2015-09-26T20:40:51","date_gmt":"2015-09-26T12:40:51","guid":{"rendered":"https:\/\/woohuiren.me\/blog\/?p=341"},"modified":"2016-03-05T00:48:07","modified_gmt":"2016-03-04T16:48:07","slug":"php-tutorial-requests","status":"publish","type":"post","link":"https:\/\/woohuiren.me\/blog\/php-tutorial-requests\/","title":{"rendered":"PHP Tutorial: Requests"},"content":{"rendered":"<p>Alright, so for this PHP tutorial, we&#8217;ll be covering requests. <\/p>\n<h2>Basics<\/h2>\n<p>The fundamental of the web and how it works is that you are able to send GET and POST requests to a web page. For the front-end, method of fetching and transferring data are usually set in HTML forms. Here&#8217;s an example usage of setting GET and POST methods in forms. <\/p>\n<pre>&lt;form method=\"POST\" action=\"login.php\"&gt;\r\n&#09;&lt;input type=\"text\" name=\"username\" placeholder=\"Username\"&gt;\r\n&#09;&lt;input type=\"password\" name=\"password\" placeholder=\"Password\"&gt;\r\n&lt;\/form&gt;\r\n<\/pre>\n<p>What about normal visiting of websites? Well, whenever you visit a web page through your web browser, your browser will send a GET request to that website to fetch its headers and content. <\/p>\n<p><a href=\"https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/php-requests-example.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/php-requests-example-1024x576.png?resize=648%2C365&#038;ssl=1\" alt=\"php-requests-example\" width=\"648\" height=\"365\" class=\"alignnone size-large wp-image-346\" srcset=\"https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/php-requests-example.png?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/php-requests-example.png?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/php-requests-example.png?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/php-requests-example.png?w=1680&amp;ssl=1 1680w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/2015-09-26_18-39-19.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/2015-09-26_18-39-19.png?resize=648%2C212&#038;ssl=1\" alt=\"2015-09-26_18-39-19\" width=\"648\" height=\"212\" class=\"alignnone size-large wp-image-343\" srcset=\"https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/2015-09-26_18-39-19.png?resize=1024%2C335&amp;ssl=1 1024w, https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/2015-09-26_18-39-19.png?resize=300%2C98&amp;ssl=1 300w, https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/2015-09-26_18-39-19.png?w=1366&amp;ssl=1 1366w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><small><em>An example of a HTTP GET request to Lowendtalk.com<\/em><\/small><\/p>\n<p>I visited the website &#8220;http:\/\/www.lowendtalk.com&#8221;, so my browser made a HTTP GET request to that website. Okay, so let&#8217;s breakdown the first example shown above.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/php-requests-example-2.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/php-requests-example-2.png?resize=648%2C365&#038;ssl=1\" alt=\"php-requests-example-2\" width=\"648\" height=\"365\" class=\"alignnone size-large wp-image-347\" srcset=\"https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/php-requests-example-2.png?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/php-requests-example-2.png?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/php-requests-example-2.png?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/php-requests-example-2.png?w=1680&amp;ssl=1 1680w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><\/p>\n<p>The method is GET, which means yeah, it&#8217;s a GET request. As for URI, it&#8217;s the &#8220;Uniform Resource Identifier&#8221;, which means the name of the resource being requested. In this example, the resource being requested from the site is &#8220;\/&#8221;. It&#8217;s a bit confusing so I&#8217;ll show another example later. The version refers to the version of the request being made. HTTP\/1.1 is a newer version of the HTTP protocol and right now, the latest HTTP protocol version being implemented is HTTP\/2.0 which most websites do not support, yet. The host refers to the hostname of the website and in this case, it&#8217;s &#8220;www.lowendtalk.com&#8221;.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/php-requests-example-3.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/php-requests-example-3.png?resize=648%2C365&#038;ssl=1\" alt=\"php-requests-example-3\" width=\"648\" height=\"365\" class=\"alignnone size-large wp-image-350\" srcset=\"https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/php-requests-example-3.png?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/php-requests-example-3.png?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/php-requests-example-3.png?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/php-requests-example-3.png?w=1680&amp;ssl=1 1680w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><\/p>\n<p>So here&#8217;s the 2nd example. By now, you should be clear about what part does the URI refers to. Now, we wanna try extracting some data in PHP.<\/p>\n<p>You can observe that the URI has some funny looking characters like &#8220;?&#8221; and &#8220;=&#8221;. The question mark means it is the start of a query string. &#8220;Wait wait wait, what&#8217;s a query string?&#8221; Well, query string is the content that is included for data processing. It is used typically in HTML forms to submit data to the server. <\/p>\n<h2>Extracting Data<\/h2>\n<p>Okay okay, now let&#8217;s <em>seriously<\/em> move on to the PHP coding part. As you can see in the query string, &#8220;q=ayylmao&#8221;. This means the key is &#8220;q&#8221; while the value for the key &#8220;q&#8221; is &#8220;ayylmao&#8221;. So how do we extract the value &#8220;ayylmao&#8221;? It&#8217;s pretty simple actually.<\/p>\n<pre>&lt;?php\r\necho $_GET[\"q\"]; \/\/this will output \"ayylmao\"\r\n?&gt;\r\n<\/pre>\n<p>So we&#8217;ve covered GET requests, what about POST requests? According to RFC 2616 [1], a GET request should be used for retrieving data from the server whilst POST request should be used for sending data to the server. But both of them are quite similar to each other in most ways. The main difference is that for GET requests, the data is shown in the browser URL but for POST requests, it isn&#8217;t. On top of that, GET requests are usually cached by your browser while POST requests are never cached. <\/p>\n<p>Since GET requests would show your entire query string in the browser URL, you would usually want to use POST requests for data sensitive data submissions such as a login or registration form [2]. This is because your friend sitting beside you would know your password if the login form&#8217;s method is a GET. However, for a search form, a GET request will do just fine. <\/p>\n<p>So let&#8217;s say the request made was a POST request, the query string data can be extracted like this.<\/p>\n<pre>&lt;?php\r\necho $_POST[\"q\"]; \/\/this will output \"ayylmao\"\r\n?&gt;\r\n<\/pre>\n<p>So to just get you a visual representation of what I&#8217;ve just said above, here&#8217;s an image.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/difference-between-get-and-post.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/difference-between-get-and-post.png?resize=648%2C365&#038;ssl=1\" alt=\"difference-between-get-and-post\" width=\"648\" height=\"365\" class=\"alignnone size-large wp-image-353\" srcset=\"https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/difference-between-get-and-post.png?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/difference-between-get-and-post.png?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/difference-between-get-and-post.png?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/difference-between-get-and-post.png?w=1680&amp;ssl=1 1680w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><\/p>\n<p>I hope you&#8217;ve understood what requests are and the differences between a GET and POST request. If you have any other questions, feel free to leave a comment below and I&#8217;ll get back to you as soon as possible. Have fun with PHP! :)<\/p>\n<p>Next Tutorial: <a href=\"https:\/\/woohuiren.me\/blog\/php-tutorial-sessions\/\">PHP Sessions<\/a><\/p>\n<p>Tutorial Overview: <a href=\"https:\/\/woohuiren.me\/blog\/php-fast-track-learning\/\">Fast Track Learning<\/a><\/p>\n<p>Sources Cited:<\/p>\n<p>[1] &#8211; <a href=\"http:\/\/tools.ietf.org\/html\/rfc2616#page-53\">http:\/\/tools.ietf.org\/html\/rfc2616#page-53<\/a><br \/>\n[2] &#8211; <a href=\"http:\/\/tools.ietf.org\/html\/rfc2616#section-15.1.3\">http:\/\/tools.ietf.org\/html\/rfc2616#section-15.1.3<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Alright, so for this PHP tutorial, we&#8217;ll be covering requests. Basics The fundamental of the web and how it works is that you are able to send GET and POST requests to a web page. For the front-end, method of fetching and transferring data are usually set in HTML forms. Here&#8217;s an example usage of &hellip; <a href=\"https:\/\/woohuiren.me\/blog\/php-tutorial-requests\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;PHP Tutorial: Requests&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":342,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"image","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[2,5,29],"tags":[30,92],"class_list":["post-341","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-information-technology","category-programming","category-tutorials","tag-php","tag-php-requests","post_format-post-format-image"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/09\/php-requests.png?fit=1920%2C1080&ssl=1","_links":{"self":[{"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/posts\/341"}],"collection":[{"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/comments?post=341"}],"version-history":[{"count":11,"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/posts\/341\/revisions"}],"predecessor-version":[{"id":515,"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/posts\/341\/revisions\/515"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/media\/342"}],"wp:attachment":[{"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/media?parent=341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/categories?post=341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/tags?post=341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}