O'Reilly logo

PHP 5 Social Networking by Michael Peacock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Consuming

As we have learned from this chapter, creating an API is a very large topic, one which is covered in greater detail in a number of books dedicated to the subject. Let's look at how we can quickly consume our new API using cURL (http://www.php.net/manual/en/intro.curl.php).

With cURL, we can pass our username and password, and it will handle passing the appropriate values to authenticate against the basic authentication we have in place.

<?php

First, we need to set our username, password, and the URL we wish to connect to.

$username = 'michael';
$password = 'password';
$url = "http://localhost/api/users";

Next, we initialize a connection to the URL.

$ch = curl_init($url);

We then set a number of options, including the username and password, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required