]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - sample-remote-api-call.txt
Add plain API format
[Github/YOURLS.git] / sample-remote-api-call.txt
1 <?php
2
3 /*
4  * YOURLS : sample file showing how to use the API
5  * This shows how to tap into your YOURLS install API from *ANOTHER* server
6  * not from a file hosted on the same server. It's just a bit dumb to make a
7  * remote HTTP request to the server the request originates from.
8  *
9  * Rename to .php
10  *
11  */
12
13 // EDIT THIS: your auth parameters
14 $username = 'joe';
15 $password = '123456';
16
17 // EDIT THIS: the query parameters
18 $url     = 'http://planetozh.com/blog/'; // URL to shrink
19 $keyword = 'ozh';                        // optional keyword
20 $title   = 'Super blog!';                // optional, if omitted YOURLS will lookup title with an HTTP request
21 $format  = 'json';                       // output format: 'json', 'xml' or 'simple'
22
23 // EDIT THIS: the URL of the API file
24 $api_url = 'http://your-own-domain-here.com/yourls-api.php';
25
26 // Init the CURL session
27 $ch = curl_init();
28 curl_setopt( $ch, CURLOPT_URL, $api_url );
29 curl_setopt( $ch, CURLOPT_HEADER, 0 );            // No header in the result
30 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // Return, do not echo result
31 curl_setopt( $ch, CURLOPT_POST, 1 );              // This is a POST request
32 curl_setopt( $ch, CURLOPT_POSTFIELDS, array(      // Data to POST
33                 'url'      => $url,
34                 'keyword'  => $keyword,
35                 'title'    => $title,
36                 'format'   => $format,
37                 'action'   => 'shorturl',
38                 'username' => $username,
39                 'password' => $password
40         ) );
41
42 // Fetch and return content
43 $data = curl_exec($ch);
44 curl_close($ch);
45
46 // Do something with the result. Here, we just echo it.
47 echo $data;
48