Template:REST API GET sample code (php+curl)

From Linux Web Expert

Revision as of 22:43, 24 December 2023 by imported>Novem Linguae (spaces inside parentheses)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

<?php

$url = "https://en.wikipedia.org/w/rest.php/some/path";


$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_USERAGENT, "MediaWiki REST API docs examples/0.1 (https://www.mediawiki.org/wiki/API_talk:REST_API)" );
$output = curl_exec( $ch );
curl_close( $ch );

echo($output);
?>
<?php
/*
Sample code
*/
$url = "https://test.example/foo";
$params = [ "x" => "1", "y" => "2" ];
$url = $url . "?" . http_build_query( $params );

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_USERAGENT, "MediaWiki REST API docs examples/0.1 (https://www.mediawiki.org/wiki/API_talk:REST_API)" );
$output = curl_exec( $ch );
curl_close( $ch );

echo($output);
?>
Template documentation