1 00:00:00,360 --> 00:00:04,670 We've been using cURL in the console to communicate with the GitHub API. 2 00:00:04,670 --> 00:00:08,670 CURL is a tool for transferring data using various protocols. 3 00:00:08,670 --> 00:00:10,880 Besides having a command line tool, 4 00:00:10,880 --> 00:00:15,310 cURL is also supported in most programming languages, including PHP. 5 00:00:16,480 --> 00:00:20,060 GitHub suggests using one of the third party wrapper libraries. 6 00:00:20,060 --> 00:00:24,860 A wrapper library is a layer of code which translates an existing interface 7 00:00:24,860 --> 00:00:29,210 such as the GitHub API into a more easily used interface. 8 00:00:29,210 --> 00:00:32,890 We'll be using a wrapper library when we write our application. 9 00:00:32,890 --> 00:00:36,730 Not all APIs will have wrapper libraries for you to use, and 10 00:00:36,730 --> 00:00:40,410 all libraries that wrap APIs will be using cURL. 11 00:00:40,410 --> 00:00:42,950 So let's start with a primer on cURL so 12 00:00:42,950 --> 00:00:45,350 you can understand the communication that's happening. 13 00:00:46,610 --> 00:00:48,800 When using the PHP cURL function, 14 00:00:48,800 --> 00:00:53,370 you initialize a cURL session using the curl_init function, 15 00:00:53,370 --> 00:00:58,650 then you can set all your options for the transfer via a curl_setopt, 16 00:00:58,650 --> 00:01:02,860 then you can execute the session with curl_exec, and 17 00:01:02,860 --> 00:01:06,730 then you finish off your session using curl_close. 18 00:01:06,730 --> 00:01:08,700 Let's jump into WorkSpaces and give it a try. 19 00:01:10,730 --> 00:01:12,555 Let's close the console and create a new file. 20 00:01:16,533 --> 00:01:17,730 We'll name this curl.php. 21 00:01:22,644 --> 00:01:25,376 We'll start with the URL we want to request. 22 00:01:25,376 --> 00:01:29,450 This is the same URL that we used in the last request. 23 00:01:33,256 --> 00:01:43,256 Https://api.github.com/users/treehouse. 24 00:01:48,521 --> 00:01:50,861 Then we're going to start with the curl_init. 25 00:01:55,443 --> 00:01:58,411 We're going to use this process variable in our next functions. 26 00:02:03,432 --> 00:02:05,273 Curl_init and pass the URL. 27 00:02:05,273 --> 00:02:09,110 We're not going to pass any options to start with. 28 00:02:09,110 --> 00:02:11,817 So we'll just call curl_exec. 29 00:02:16,311 --> 00:02:17,876 And pass the process. 30 00:02:17,876 --> 00:02:21,625 And then we can do a curl_close. 31 00:02:27,193 --> 00:02:28,596 Now let's take a look at this in the browser. 32 00:02:28,596 --> 00:02:35,420 The site doesn't work yet because we haven't run the composer install. 33 00:02:35,420 --> 00:02:36,700 We'll get back to that in a little bit. 34 00:02:37,790 --> 00:02:39,791 Let's start by going to curl.php. 35 00:02:45,386 --> 00:02:49,394 It says that the request is forbidden and requires a user agent. 36 00:02:49,394 --> 00:02:51,311 So let's add that as an option. 37 00:02:57,010 --> 00:03:01,912 Curl_setopt, our process, 38 00:03:01,912 --> 00:03:05,687 CURLOPT_USERAGENT. 39 00:03:09,085 --> 00:03:10,489 And then we're going to pass a username. 40 00:03:13,349 --> 00:03:14,589 Let's set the username at the top. 41 00:03:18,210 --> 00:03:19,621 We'll set this to mikethefrog. 42 00:03:23,402 --> 00:03:24,464 Now let's go back to the browser. 43 00:03:28,370 --> 00:03:29,051 Great. 44 00:03:29,051 --> 00:03:31,236 Now the results are displayed directly to the screen. 45 00:03:31,236 --> 00:03:32,965 Let's set another option so 46 00:03:32,965 --> 00:03:37,009 that we can store the results in a variable that we can manipulate. 47 00:03:43,305 --> 00:03:48,021 Curl_setopt, our process, and 48 00:03:48,021 --> 00:03:53,717 this time, CURLOPT_RETURNTRANSFER. 49 00:03:53,717 --> 00:03:57,566 And we'll set this to 1 or true. 50 00:03:57,566 --> 00:04:03,073 Next, we'll put the return of the curl_exec into a variable. 51 00:04:07,461 --> 00:04:09,633 And then we can run the json_decode on those results. 52 00:04:19,231 --> 00:04:21,438 Let's var_dump the results to see what we have. 53 00:04:33,550 --> 00:04:36,235 The json_decode put the results into an object. 54 00:04:36,235 --> 00:04:41,640 So now we can access the properties just like we would any other object. 55 00:04:41,640 --> 00:04:42,581 Let's display the avatar. 56 00:04:49,224 --> 00:04:53,456 Echo, image source equals 57 00:04:55,971 --> 00:05:01,863 We use our results and pull the avatar_url. 58 00:05:14,462 --> 00:05:17,311 Great job using PHP to communicate with an API. 59 00:05:17,311 --> 00:05:22,310 All restful APIs you encounter should work pretty much the same way. 60 00:05:22,310 --> 00:05:25,480 Try using cURL to communicate with another API for 61 00:05:25,480 --> 00:05:29,290 sites that you use, like Twitter, Facebook or Google. 62 00:05:29,290 --> 00:05:32,370 In the next section, we'll be using a wrapper library and 63 00:05:32,370 --> 00:05:34,780 set up token-based authentication so 64 00:05:34,780 --> 00:05:38,850 that our application can pull private data and make updates to our account.