Skip to content

Instantly share code, notes, and snippets.

@Showfom
Last active April 29, 2019 07:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Showfom/1f7f53d64a9ddd0b7e31ec4831dd0789 to your computer and use it in GitHub Desktop.
Save Showfom/1f7f53d64a9ddd0b7e31ec4831dd0789 to your computer and use it in GitHub Desktop.
U.NU Sample Remote API Call
<?php
/*
* 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 https://u.nu/tg
//$password = 'password'; // apply for Premium API access here https://u.nu/tg
// EDIT THIS: the query parameters
$url = 'https://example.com/'; // 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 = 'https://u.nu/api.php';
// 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);
curl_close($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