{"id":2725,"date":"2017-06-09T13:30:55","date_gmt":"2017-06-09T13:30:55","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2725"},"modified":"2017-06-09T13:30:55","modified_gmt":"2017-06-09T13:30:55","slug":"1-before-getting-started","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2017\/06\/09\/1-before-getting-started\/","title":{"rendered":"1 &#8211; Before Getting Started"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushPhp.js\"><\/script>\n<p style=\"text-align: center;\"><strong>This is article 1 of the YouTube API With PHP series.<\/strong><\/p>\n<p><strong>PROGRAMMING LEVEL<\/strong><\/p>\n<p>As a PHP programmer, you are expected to be familiar with the following concepts to use the YouTube API:<\/p>\n<ul>\n<li>Using cURL to make HTTP GET and POST requests<\/li>\n<li>Handle data in the JSON format (explained in the next section)<\/li>\n<li>Familiarity with associative arrays<\/li>\n<\/ul>\n<p>Actually if you go through the official YouTube API documentation, it looks very complex and intimidating, but it is not really like that. This book is meant to make it simple and logical.<\/p>\n<p>You also need to be conversant with Javascript as some of the API components like the Player API only work with Javascript. Knowledge of JQuery is definitely a plus point but not compulsory in this case.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>THE JSON DATA FORMAT<\/strong><\/p>\n<p>All data returned by the YouTube API is in the JSON format.\u00a0 JSON stands for Javascript Object Notation and is a very popular way of storing and retrieving data across many platforms and languages. The main advantage of JSON is that it is plain text using standard ASCII code, which means that it is usable on any computer or operating system.<\/p>\n<p>PHP provides native support for JSON handling since version 5.2. If you are already familiar on how to work with JSON data in PHP, then you can skip this entire article. If not then you can go through the small tutorial section below:<\/p>\n<p><strong>JSON Functions<\/strong><\/p>\n<p><strong>\u00a0<\/strong>PHP provides 4 built-in functions to work with JSON.<\/p>\n<ul>\n<li><strong>json_decode()<\/strong> &#8211; Decode a JSON string<\/li>\n<li><strong>json_encode()<\/strong> &#8211; Encode a string into JSON<\/li>\n<li><strong>json_last_error_msg()<\/strong> &#8211; the last error message during a json_decode() or json_encode function<\/li>\n<li><strong>json_last_error()<\/strong> &#8211; the last error code during a json_decode() or json_encode function<\/li>\n<\/ul>\n<p><strong>Encoding to JSON<\/strong><\/p>\n<p>Let\u2019s run this code:<br \/>\n<pre class=\"brush: php\">echo(json_encode(&quot;This is a string&quot;));<\/pre><\/p>\n<p>&nbsp;<\/p>\n<p>Output:<\/p>\n<p><span style=\"color: #999999;\">\u201cThis is a string\u201d<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Now, as you can see, encoding any primitive data type like a string or integer as JSON will always show it as a string, which is not of any practical use.<\/p>\n<p>JSON encoding is only useful when used with data structures, like a class or an array. What JSON encoding does is that it converts everything into a string based structure.<\/p>\n<p>The code below shows how this works with various data structures:<br \/>\n<pre class=\"brush: php\">&lt;?php\r\nerror_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_WARNING);\r\n$s = &quot;This is a string&quot;;\r\necho(json_encode($s) . &quot;&lt;br&gt;&quot;);\r\n$arr = array(&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;);\r\necho(json_encode($arr) . &quot;&lt;br&gt;&quot;);\r\n\r\n$arr2 = array(&quot;one&quot;=&gt;100, &quot;two&quot;=&gt;200, &quot;three&quot;=&gt;300);\r\necho(json_encode($arr2) . &quot;&lt;br&gt;&quot;);\r\n\r\n\r\n$obj = new MyClass();\r\n$obj-&gt;id = 1;\r\n$obj-&gt;name = &quot;My Name&quot;;\r\n$obj-&gt;registration = date(&quot;Y-m-d&quot;);\r\n\r\necho(json_encode($obj) . &quot;&lt;Br&gt;&quot;);\r\n\r\n\/\/\/\/\/\/\/\/\/\/\r\n\r\n\r\nclass MyClass {\r\npublic $id = 0;\r\npublic $name = &quot;&quot;;\r\npublic $registration = null;\r\n\r\n}\r\n\r\n\r\n?&gt;<\/pre><\/p>\n<p>&nbsp;<\/p>\n<p>The output:<\/p>\n<p><span style=\"color: #999999;\">&#8220;This is a string&#8221;<\/span><\/p>\n<p><span style=\"color: #999999;\">[&#8220;One&#8221;,&#8221;Two&#8221;,&#8221;Three&#8221;]<\/span><\/p>\n<p><span style=\"color: #999999;\">{&#8220;one&#8221;:100,&#8221;two&#8221;:200,&#8221;three&#8221;:300}<\/span><\/p>\n<p><span style=\"color: #999999;\">{&#8220;id&#8221;:1,&#8221;name&#8221;:&#8221;My Name&#8221;,&#8221;registration&#8221;:&#8221;2017-05-15&#8243;}<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Note that all results of json_encode() are strings.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Decoding from JSON<\/strong><\/p>\n<p>To convert JSON data back into its original structural format we use the json_decode() function. The code below runs a json_decode on various data types, after running a json_encode on them.<br \/>\n<pre class=\"brush: php\">&lt;?php\r\nerror_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_WARNING);\r\n$s = &quot;This is a string&quot;;\r\n$sJSON = json_encode($s);\r\n$arr = array(&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;);\r\n$arrJSON = json_encode($arr);\r\n$arr2 = array(&quot;one&quot;=&gt;100, &quot;two&quot;=&gt;200, &quot;three&quot;=&gt;300);\r\n$arr2JSON = json_encode($arr2);\r\n\r\n\r\n$obj = new MyClass();\r\n$obj-&gt;id = 1;\r\n$obj-&gt;name = &quot;My Name&quot;;\r\n$obj-&gt;registration = date(&quot;Y-m-d&quot;);\r\n\r\n$classJSON = json_encode($obj);\r\n\r\necho(json_decode($sJSON) . &quot;&lt;br&gt;&quot;);\r\necho(json_decode($arrJSON). &quot;&lt;br&gt;&quot;);\r\necho(json_decode($arr2JSON).  &quot;&lt;br&gt;&quot;);\r\necho(json_decode($classJSON).  &quot;&lt;br&gt;&quot;);\r\n\r\n\/\/\/\/\/\/\/\/\/\/\r\n\r\nclass MyClass {\r\npublic $id = 0;\r\npublic $name = &quot;&quot;;\r\npublic $registration = null;\r\n\r\n}\r\n\r\n\r\n?&gt;<\/pre><\/p>\n<p><em>\u00a0<\/em><\/p>\n<p>The Output:<\/p>\n<p><span style=\"color: #999999;\">This is a string<\/span><\/p>\n<p><span style=\"color: #999999;\">Array<\/span><\/p>\n<p><span style=\"color: #999999;\"><strong>Catchable fatal error<\/strong>: Object of class stdClass could not be converted to string in <strong>\/var\/websites\/test\/YouTubeapi\/json\/test-json2.php<\/strong> on line <strong>24<\/strong><\/span><\/p>\n<p><strong>\u00a0<\/strong>The string converted back into a string. The array shows as Array because we are trying to use echo() to display an array.<\/p>\n<p>However the second array failed to be decoded. $arr2 is an associative array. By default json_decode() tries to decode structures into stdClass instead of an array. Since the original structure was an array, it fails when json_decode tries to convert it into stdClass. To force json_decode to generate an associative array, we pass a second parameter as true. The second parameter is $assoc=false by default. So by passing it as true, we get our associative array back.<\/p>\n<p>&nbsp;<\/p>\n<p>So if we change<\/p>\n<p><pre class=\"brush: php\">echo(json_decode($arr2JSON).  &quot;&lt;br&gt;&quot;);<\/pre><\/p>\n<p>to<\/p>\n<p><pre class=\"brush: php\">echo(json_decode($arr2JSON, true).  &quot;&lt;br&gt;&quot;);<\/pre><\/p>\n<p>then the output becomes:<\/p>\n<p>This is a string<\/p>\n<p><span style=\"color: #999999;\">Array<\/span><\/p>\n<p><span style=\"color: #999999;\">Array<\/span><\/p>\n<p><span style=\"color: #999999;\"><strong>Catchable fatal error<\/strong>: Object of class stdClass could not be converted to string in <strong>\/var\/websites\/test\/YouTubeapi\/json\/test-json2.php<\/strong> on line <strong>25<\/strong><\/span><\/p>\n<p><span style=\"color: #999999;\"><strong>\u00a0<\/strong><\/span><\/p>\n<p>Now we have to see why the class did not get decoded. stdClass is the generic default empty class in PHP, but unlike in Java where the default Object is the parent\u00a0 of every user-defined class , stdClass is not the universal parent of a user-defined class. So the class MyClass cannot be cast into stdClass and an exception is thrown. To get back the original $obj from $classJSON , we have to do two things:<\/p>\n<ol>\n<li>Get an associative array back from json_decode<\/li>\n<li>Convert that associate array into an object of MyClass<\/li>\n<\/ol>\n<p>We need two lines of code for the above:<br \/>\n<pre class=\"brush: php\">$classAsArray = json_decode($classJSON, true);\r\n$obj = new MyClass();\r\nforeach ($classAsArray as $key =&gt; $value)\r\n $obj-&gt;{$key} = $value;\r\n\r\n\r\n<\/pre><br \/>\nLet us replace all the echo statements with var_dump and then see the output:<\/p>\n<p><em>\u00a0<\/em><br \/>\n<pre class=\"brush: php\">&lt;?php\r\n\r\nerror_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_WARNING);\r\n\r\n\r\n$s = &quot;This is a string&quot;;\r\n\r\n$sJSON = json_encode($s);\r\n\r\n\r\n$arr = array(&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;);\r\n\r\n$arrJSON = json_encode($arr);\r\n\r\n\r\n$arr2 = array(&quot;one&quot;=&gt;100, &quot;two&quot;=&gt;200, &quot;three&quot;=&gt;300);\r\n\r\n$arr2JSON = json_encode($arr2);\r\n\r\n\r\n\r\n$obj = new MyClass();\r\n\r\n$obj-&gt;id = 1;\r\n\r\n$obj-&gt;name = &quot;My Name&quot;;\r\n\r\n$obj-&gt;registration = date(&quot;Y-m-d&quot;);\r\n\r\n\r\n$classJSON = json_encode($obj);\r\n\r\n\r\n\r\nvar_dump(json_decode($sJSON));\r\n\r\necho(&quot;&lt;br&gt;&quot;);\r\n\r\nvar_dump(json_decode($arrJSON));\r\n\r\necho(&quot;&lt;br&gt;&quot;);\r\n\r\n\r\nvar_dump(json_decode($arr2JSON, true));\r\n\r\necho(&quot;&lt;br&gt;&quot;);\r\n\r\n\r\n$classAsArray = json_decode($classJSON, true);\r\n\r\n$obj = new MyClass();\r\n\r\nforeach ($classAsArray as $key =&gt; $value)\r\n\r\n$obj-&gt;{$key} = $value;\r\n\r\n\r\nvar_dump($obj);\r\n\r\necho(&quot;&lt;br&gt;&quot;);\r\n\r\n\r\n\/\/\/\/\/\/\/\/\/\/\r\n\r\n\r\nclass MyClass {\r\n\r\npublic $id = 0;\r\n\r\npublic $name = &quot;&quot;;\r\n\r\npublic $registration = null;\r\n\r\n}\r\n\r\n\r\n?&gt;<\/pre><\/p>\n<p><em>\u00a0<\/em><\/p>\n<p>The output:<\/p>\n<p><span style=\"color: #999999;\">string(16) &#8220;This is a string&#8221;<\/span><\/p>\n<p><span style=\"color: #999999;\">array(3) { [0]=&gt; string(3) &#8220;One&#8221; [1]=&gt; string(3) &#8220;Two&#8221; [2]=&gt; string(5) &#8220;Three&#8221; }<\/span><\/p>\n<p><span style=\"color: #999999;\">array(3) { [&#8220;one&#8221;]=&gt; int(100) [&#8220;two&#8221;]=&gt; int(200) [&#8220;three&#8221;]=&gt; int(300) }<\/span><\/p>\n<p><span style=\"color: #999999;\">object(MyClass)#2 (3) { [&#8220;id&#8221;]=&gt; int(1) [&#8220;name&#8221;]=&gt; string(7) &#8220;My Name&#8221; [&#8220;registration&#8221;]=&gt; string(10) &#8220;2017-05-15&#8221; }<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Error Handling<\/strong><\/p>\n<p>Normally we can check if a json_encode() or json_decode worked by checking the result. If it is null then it means there was an error. If you need to know the reason for the failure, you can use the json_last_error() and json_last_error_msg() functions.<\/p>\n<p>&nbsp;<\/p>\n<p>The first gives you an error code . You can find a list of the error codes on <a href=\"http:\/\/php.net\/manual\/en\/function.json-last-error.php\">http:\/\/php.net\/manual\/en\/function.json-last-error.php<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>The second function gives you a string description of the error message.<br \/>\n<em>\u00a0<\/em><br \/>\n<pre class=\"brush: php\"> \r\n&lt;?php\r\nerror_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_WARNING);\r\n$arr2 = &quot;{asdasd:asdasdas+10101&quot;;\r\n$jsonToArr = json_decode($arr2);\r\n\r\necho(&quot;result=&quot; . $jsonToArr . &quot;&lt;br&gt;&quot;);\r\necho(&quot;Error=&quot; . json_last_error() . &quot;&lt;br&gt;&quot;);\r\necho(&quot;Error Message =&quot; . json_last_error_msg() . &quot;&lt;br&gt;&quot;);\r\n\r\n?&gt;<\/pre><\/p>\n<p>The Output:<\/p>\n<p><span style=\"color: #999999;\">result=<\/span><\/p>\n<p><span style=\"color: #999999;\">Error=4<\/span><\/p>\n<p><span style=\"color: #999999;\">Error Message =Syntax error<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>This concludes our small tutorial on how to handle JSON data in PHP.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>This is article 1 of the YouTube API With PHP series. PROGRAMMING LEVEL As a PHP programmer, you are expected to be familiar with the <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2017\/06\/09\/1-before-getting-started\/\" title=\"1 &#8211; Before Getting Started\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2727,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[318],"tags":[],"class_list":["post-2725","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-youtube-api-with-php"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2725","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2725"}],"version-history":[{"count":8,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2725\/revisions"}],"predecessor-version":[{"id":2734,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2725\/revisions\/2734"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2727"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}