Created
September 13, 2019 15:07
-
-
Save fgrweb/dbf714b0c4b3d9d2b07965d1e7267217 to your computer and use it in GitHub Desktop.
Get WordPress API data with curl, sending Barer Token
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$curl = curl_init(); | |
curl_setopt_array( | |
$curl, | |
array( | |
CURLOPT_URL => "https://example.com/wp-json/wp/v2/evento?fields=id,title,modified_gmt,validate,status,from,until,location,web&per_page=20", | |
CURLOPT_RETURNTRANSFER => true, | |
CURLINFO_HEADER_OUT => true, | |
CURLOPT_ENCODING => "", | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 30, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => "GET", | |
CURLOPT_HTTPHEADER => array( | |
"authorization: Bearer Token", | |
"cache-control: no-cache" | |
), | |
) | |
); | |
$response = curl_exec( $curl ); | |
$err = curl_error($curl); | |
curl_close($curl); | |
if ($err) { | |
echo "cURL Error #:" . $err; | |
} else { | |
$data = json_decode( $response, true, 512, JSON_OBJECT_AS_ARRAY ); | |
echo '<ul>'; | |
foreach ($data as $key => $dato) { | |
echo '<li>Titulo ' . $dato['title']['rendered'] . '</li>'; | |
} | |
echo '</ul>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment