]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - sample-remote-api-call.php.txt
More comments about YOURLS_PRIVATE
[Github/YOURLS.git] / sample-remote-api-call.php.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 \r
10 // EDIT THIS: your auth parameters\r
11 $username = 'joe';\r
12 $password = '123456';\r
13 \r
14 // EDIT THIS: the query parameters\r
15 $url = 'http://planetozh.com/blog/'; // URL to shrink\r
16 $keyword = 'ozh';                               // optional keyword\r
17 $format = 'json';                               // output format: 'json', 'xml' or 'simple'\r
18 \r
19 // EDIT THIS: the URL of the API file\r
20 $api_url = 'http://yoursite/yourls-api.php';\r
21 \r
22 // Init the CURL session\r
23 $ch = curl_init();\r
24 curl_setopt($ch, CURLOPT_URL, $api_url);\r
25 curl_setopt($ch, CURLOPT_HEADER, 0);            // No header in the result\r
26 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result\r
27 curl_setopt($ch, CURLOPT_POST, 1);              // This is a POST request\r
28 curl_setopt($ch, CURLOPT_POSTFIELDS, array(     // Data to POST\r
29                 'url'      => $url,\r
30                 'keyword'  => $keyword,\r
31                 'format'   => $format,\r
32                 'action'   => 'shorturl',\r
33                 'username' => $username,\r
34                 'password' => $password\r
35         ));\r
36 \r
37 // Fetch and return content\r
38 $data = curl_exec($ch);\r
39 curl_close($ch);\r
40 \r
41 // Do something with the result. Here, we just echo it.\r
42 echo $data;\r
43 \r
44 ?>