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