]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - yourls-api.php
More comments
[Github/YOURLS.git] / yourls-api.php
1 <?php\r
2 define('YOURLS_API', true);\r
3 require_once( dirname(__FILE__).'/includes/load-yourls.php' );\r
4 yourls_maybe_require_auth();\r
5 \r
6 $action = ( isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : null );\r
7 \r
8 yourls_do_action( 'api', $action );\r
9         \r
10 switch( $action ) {\r
11 \r
12         // Shorten a URL\r
13         case 'shorturl':\r
14                 $url = ( isset( $_REQUEST['url'] ) ? $_REQUEST['url'] : '' );\r
15                 $keyword = ( isset( $_REQUEST['keyword'] ) ? $_REQUEST['keyword'] : '' );\r
16                 $title = ( isset( $_REQUEST['title'] ) ? $_REQUEST['title'] : '' );\r
17                 $return = yourls_add_new_link( $url, $keyword, $title );\r
18                 $return['simple'] = ( isset( $return['shorturl'] ) ? $return['shorturl'] : '' ); // This one will be used in case output mode is 'simple'\r
19                 unset( $return['html'] ); // in API mode, no need for our internal HTML output\r
20                 break;\r
21         \r
22         // Global stats\r
23         case 'stats':\r
24                 $filter = ( isset( $_REQUEST['filter'] ) ? $_REQUEST['filter'] : '' );\r
25                 $limit = ( isset( $_REQUEST['limit'] ) ? $_REQUEST['limit'] : '' );\r
26                 $return = yourls_api_stats( $filter, $limit );\r
27                 break;\r
28         \r
29         // Stats for a shorturl\r
30         case 'url-stats':\r
31                 $shorturl = ( isset( $_REQUEST['shorturl'] ) ? $_REQUEST['shorturl'] : '' );\r
32                 $return = yourls_api_url_stats( $shorturl );\r
33                 break;\r
34 \r
35         // Expand a short link\r
36         case 'expand':\r
37                 $shorturl = ( isset( $_REQUEST['shorturl'] ) ? $_REQUEST['shorturl'] : '' );\r
38                 $return = yourls_api_expand( $shorturl );\r
39                 break;\r
40         \r
41         // Missing or incorrect action parameter\r
42         default:\r
43                 $return = array(\r
44                         'errorCode' => 400,\r
45                         'message'   => 'Unknown or missing "action" parameter',\r
46                         'simple'    => 'Unknown or missing "action" parameter',\r
47                 );\r
48                 \r
49 \r
50 }\r
51 \r
52 $format = ( isset( $_REQUEST['format'] ) ? $_REQUEST['format'] : 'xml' );\r
53 \r
54 yourls_api_output( $format, $return );\r
55 \r
56 die();