Communication with cURL5:39 with Alena Holligan
cURL is a tool for transferring data using various protocols. cURL allows you to communicate with external services before ever sending any information to a browser. When using the PHP cURL functions, you initialize a cURL session, set all your options for the transfer, execute the session and finally close your session.
cURL is a computer software project providing a library and command-line tool for transferring data using various protocols. The cURL project produces two products, libcurl and cURL. It was first released in 1997. The name originally stood for "see URL". read more
PHP supports libcurl a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP's ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication.
Using cURL with PHP
Most of the time PHP is compiled with cURL support, you can begin using the cURL functions without any additional setup. The basic idea behind the cURL functions is that you initialize a cURL session using the curl_init(), then you can set all your options for the transfer via the curl_setopt(), then you can execute the session with the curl_exec() and then you finish off your session using the curl_close(). Here is an example that uses the cURL functions to fetch the example.com homepage into a file:
Example Using PHP's cURL module to fetch the example.com homepage
<?php $ch = curl_init("http://www.example.com/"); $fp = fopen("example_homepage.txt", "w"); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp); ?>
We've been using cURL in the console to communicate with the GitHub API. 0:00 CURL is a tool for transferring data using various protocols. 0:04 Besides having a command line tool, 0:08 cURL is also supported in most programming languages, including PHP. 0:10 GitHub suggests using one of the third party wrapper libraries. 0:16 A wrapper library is a layer of code which translates an existing interface 0:20 such as the GitHub API into a more easily used interface. 0:24 We'll be using a wrapper library when we write our application. 0:29 Not all APIs will have wrapper libraries for you to use, and 0:32 all libraries that wrap APIs will be using cURL. 0:36 So let's start with a primer on cURL so 0:40 you can understand the communication that's happening. 0:42 When using the PHP cURL function, 0:46 you initialize a cURL session using the curl_init function, 0:48 then you can set all your options for the transfer via a curl_setopt, 0:53 then you can execute the session with curl_exec, and 0:58 then you finish off your session using curl_close. 1:02 Let's jump into WorkSpaces and give it a try. 1:06 Let's close the console and create a new file. 1:10 We'll name this curl.php. 1:16 We'll start with the URL we want to request. 1:22 This is the same URL that we used in the last request. 1:25 Https://api.github.com/users/treehouse. 1:33 Then we're going to start with the curl_init. 1:48 We're going to use this process variable in our next functions. 1:55 Curl_init and pass the URL. 2:03 We're not going to pass any options to start with. 2:05 So we'll just call curl_exec. 2:09 And pass the process. 2:16 And then we can do a curl_close. 2:17 Now let's take a look at this in the browser. 2:27 The site doesn't work yet because we haven't run the composer install. 2:28 We'll get back to that in a little bit. 2:35 Let's start by going to curl.php. 2:37 It says that the request is forbidden and requires a user agent. 2:45 So let's add that as an option. 2:49 Curl_setopt, our process, 2:57 CURLOPT_USERAGENT. 3:01 And then we're going to pass a username. 3:09 Let's set the username at the top. 3:13 We'll set this to mikethefrog. 3:18 Now let's go back to the browser. 3:23 Great. 3:28 Now the results are displayed directly to the screen. 3:29 Let's set another option so 3:31 that we can store the results in a variable that we can manipulate. 3:32 Curl_setopt, our process, and 3:43 this time, CURLOPT_RETURNTRANSFER. 3:48 And we'll set this to 1 or true. 3:53 Next, we'll put the return of the curl_exec into a variable. 3:57 And then we can run the json_decode on those results. 4:07 Let's var_dump the results to see what we have. 4:19 The json_decode put the results into an object. 4:33 So now we can access the properties just like we would any other object. 4:36 Let's display the avatar. 4:41 Echo, image source equals 4:49 We use our results and pull the avatar_url. 4:55 Great job using PHP to communicate with an API. 5:14 All restful APIs you encounter should work pretty much the same way. 5:17 Try using cURL to communicate with another API for 5:22 sites that you use, like Twitter, Facebook or Google. 5:25 In the next section, we'll be using a wrapper library and 5:29 set up token-based authentication so 5:32 that our application can pull private data and make updates to our account. 5:34
You need to sign up for Treehouse in order to download course files.Sign up