Skip to content

Instantly share code, notes, and snippets.

@aliboy08
Last active November 2, 2018 12:09
Show Gist options
  • Save aliboy08/21e8a5578a7d016f1584cceafbef4bac to your computer and use it in GitHub Desktop.
Save aliboy08/21e8a5578a7d016f1584cceafbef4bac to your computer and use it in GitHub Desktop.
<?php
$url = 'https://fivebyfive.com.au/wp-json/wp/v2/posts';
// Using file_get_contents
// Some hostings might disable this for security purposes
$result = file_get_contents($url);
$result = json_decode($result);
foreach( $result as $post ) {
echo '<pre>'. print_r($post->title->rendered, true) . '</pre>';
}
// Using url
// Initiate curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable SSL verification
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_URL,$url); // Set the url
$result = curl_exec($ch); // Execute
curl_close($ch); // Closing
$result = json_decode($result);
foreach( $result as $post ) {
echo '<pre>'. print_r($post->title->rendered, true) . '</pre>';
}
// Curl post json example:
$data = array("name" => "Hagrid", "age" => "36");
$data_string = json_encode($data);
$ch = curl_init('http://api.local/rest/users');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment