4 * Functions for the API
6 * Note about translation : this file should NOT be translation ready
7 * API messages and returns are supposed to be programmatically tested, so default English is expected
12 * API function wrapper: Shorten a URL
15 * @return array Result of API call
17 function yourls_api_action_shorturl() {
18 $url = ( isset( $_REQUEST['url'] ) ? $_REQUEST['url'] : '' );
19 $keyword = ( isset( $_REQUEST['keyword'] ) ? $_REQUEST['keyword'] : '' );
20 $title = ( isset( $_REQUEST['title'] ) ? $_REQUEST['title'] : '' );
21 $return = yourls_add_new_link( $url, $keyword, $title );
22 $return['simple'] = ( isset( $return['shorturl'] ) ? $return['shorturl'] : '' ); // This one will be used in case output mode is 'simple'
23 unset( $return['html'] ); // in API mode, no need for our internal HTML output
24 return yourls_apply_filter( 'api_result_shorturl', $return );
28 * API function wrapper: Stats about links (XX top, bottom, last, rand)
31 * @return array Result of API call
33 function yourls_api_action_stats() {
34 $filter = ( isset( $_REQUEST['filter'] ) ? $_REQUEST['filter'] : '' );
35 $limit = ( isset( $_REQUEST['limit'] ) ? $_REQUEST['limit'] : '' );
36 $start = ( isset( $_REQUEST['start'] ) ? $_REQUEST['start'] : '' );
37 return yourls_apply_filter( 'api_result_stats', yourls_api_stats( $filter, $limit, $start ) );
41 * API function wrapper: Just the global counts of shorturls and clicks
44 * @return array Result of API call
46 function yourls_api_action_db_stats() {
47 return yourls_apply_filter( 'api_result_db_stats', yourls_api_db_stats() );
51 * API function wrapper: Stats for a shorturl
54 * @return array Result of API call
56 function yourls_api_action_url_stats() {
57 $shorturl = ( isset( $_REQUEST['shorturl'] ) ? $_REQUEST['shorturl'] : '' );
58 return yourls_apply_filter( 'api_result_url_stats', yourls_api_url_stats( $shorturl ) );
62 * API function wrapper: Expand a short link
65 * @return array Result of API call
67 function yourls_api_action_expand() {
68 $shorturl = ( isset( $_REQUEST['shorturl'] ) ? $_REQUEST['shorturl'] : '' );
69 return yourls_apply_filter( 'api_result_expand', yourls_api_expand( $shorturl ) );
73 * API function wrapper: return version numbers
76 * @return array Result of API call
78 function yourls_api_action_version() {
79 $return['version'] = $return['simple'] = YOURLS_VERSION;
80 if( isset( $_REQUEST['db'] ) && $_REQUEST['db'] == 1 )
81 $return['db_version'] = YOURLS_DB_VERSION;
82 return yourls_apply_filter( 'api_result_version', $return );
86 * Return API result. Dies after this
89 function yourls_api_output( $mode, $return ) {
90 if( isset( $return['simple'] ) ) {
91 $simple = $return['simple'];
92 unset( $return['simple'] );
95 yourls_do_action( 'pre_api_output', $mode, $return );
97 if( isset( $return['statusCode'] ) ) {
98 $code = $return['statusCode'];
99 } elseif ( isset( $return['errorCode'] ) ) {
100 $code = $return['errorCode'];
104 yourls_status_header( $code );
108 yourls_content_type_header( 'application/javascript' );
109 echo $return['callback'] . '(' . json_encode( $return ) . ')';
113 yourls_content_type_header( 'application/json' );
114 echo json_encode( $return );
118 yourls_content_type_header( 'application/xml' );
119 echo yourls_xml_encode( $return );
124 yourls_content_type_header( 'text/plain' );
125 if( isset( $simple ) )
130 yourls_do_action( 'api_output', $mode, $return );
136 * Return array for API stat requests
139 function yourls_api_stats( $filter = 'top', $limit = 10, $start = 0 ) {
140 $return = yourls_get_stats( $filter, $limit, $start );
141 $return['simple'] = 'Need either XML or JSON format for stats';
142 $return['message'] = 'success';
143 return yourls_apply_filter( 'api_stats', $return, $filter, $limit, $start );
147 * Return array for counts of shorturls and clicks
150 function yourls_api_db_stats() {
152 'db-stats' => yourls_get_db_stats(),
154 'simple' => 'Need either XML or JSON format for stats',
155 'message' => 'success',
158 return yourls_apply_filter( 'api_db_stats', $return );
162 * Return array for API stat requests
165 function yourls_api_url_stats( $shorturl ) {
166 $keyword = str_replace( YOURLS_SITE . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc'
167 $keyword = yourls_sanitize_string( $keyword );
169 $return = yourls_get_link_stats( $keyword );
170 $return['simple'] = 'Need either XML or JSON format for stats';
171 return yourls_apply_filter( 'api_url_stats', $return, $shorturl );
175 * Expand short url to long url
178 function yourls_api_expand( $shorturl ) {
179 $keyword = str_replace( YOURLS_SITE . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc'
180 $keyword = yourls_sanitize_string( $keyword );
182 $longurl = yourls_get_keyword_longurl( $keyword );
186 'keyword' => $keyword,
187 'shorturl' => YOURLS_SITE . "/$keyword",
188 'longurl' => $longurl,
189 'title' => yourls_get_keyword_title( $keyword ),
190 'simple' => $longurl,
191 'message' => 'success',
196 'keyword' => $keyword,
197 'simple' => 'not found',
198 'message' => 'Error: short URL not found',
203 return yourls_apply_filter( 'api_expand', $return, $shorturl );