Skip Navigation

Pulling Data from SWAPI with PHP

This is small snippet to show you how you might get started pulling data from an api with php.
<?php

// pass a url
function getStuff($url){
	// go get the content from that url
	// then convert the content to a php object
	return json_decode(file_get_contents($url));
}

// get the person data
$person = getStuff('https://swapi.co/api/people/3');
print_r($person);

?>

<h1><?php echo $person->name; ?></h1>
<p> <?php echo $person->height; ?> <br>
	<?php echo $person->hair_color; ?></p>

<?php

// get the homeworld of the user
 $homeworld = getStuff($person->homeworld);
 print_r($homeworld);

Related Snippets

See all