Skip to content

Instantly share code, notes, and snippets.



Last active Apr 29, 2019
What would you like to do?
U.NU Sample Remote API Call
* YOURLS : sample file showing how to use the API
* This shows how to tap into your YOURLS install API from *ANOTHER* server
* not from a file hosted on the same server. It's just a bit dumb to make a
* remote HTTP request to the server the request originates from.
* Rename to .php
// EDIT THIS: your auth parameters
//$username = 'username'; // apply for Premium API access here
//$password = 'password'; // apply for Premium API access here
// EDIT THIS: the query parameters
$url = ''; // URL to shrink
$keyword = 'example'; // optional keyword
$title = 'Example'; // optional, if omitted YOURLS will lookup title with an HTTP request
$format = 'json'; // output format: 'json', 'xml' or 'simple'
// EDIT THIS: the URL of the API file
$api_url = '';
// Init the CURL session
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $api_url );
curl_setopt( $ch, CURLOPT_HEADER, 0 ); // No header in the result
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // Return, do not echo result
curl_setopt( $ch, CURLOPT_POST, 1 ); // This is a POST request
curl_setopt( $ch, CURLOPT_POSTFIELDS, array( // Data to POST
'url' => $url,
'keyword' => $keyword,
'title' => $title,
'format' => $format,
'action' => 'shorturl'
// 'username' => $username,
// 'password' => $password
) );
// Fetch and return content
$data = curl_exec($ch);
// Do something with the result. Here, we just echo it.
echo $data;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment