Skip to content

Commit

Permalink
Improving the API functions and API sample file to reflect the new au…
Browse files Browse the repository at this point in the history
…th system

git-svn-id: http://yourls.googlecode.com/svn/trunk@22 12232710-3e20-11de-b438-597f59cd7555
  • Loading branch information
ozh committed May 21, 2009
1 parent bb901d6 commit ce48a01
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 35 deletions.
45 changes: 29 additions & 16 deletions test-api.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
<?php

// Auth parameters
$login = 'joe';
$pass = '123456';
/*
* YOURLS : sample file showing how to use the API
*/

// POST parameters
$post_data = array( 'url' => 'http://planetozh.com/', 'keyword'=>'ozh', 'format'=>'json' );
// EDIT THIS: your auth parameters
$username = 'joe';
$password = '123456';

// EDIT THIS: the query parameters
$url = 'http://planetozh.com/caca'; // URL to shrink
$keyword = 'caca'; // optional keyword
$format = 'json'; // output format: 'json', 'xml' or 'simple'

// EDIT THIS: the URL of the API file
$api_url = 'http://127.0.0.1/ozh.in/yourls-api.php';

// Init the CURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1/ozh.in/yourls-api.php"); // URL of the API file
curl_setopt($ch, CURLOPT_HEADER, 0); // No header in the result
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); // AUTH with any (best) method available
curl_setopt($ch, CURLOPT_USERPWD, "$login:$pass"); // login/pwd as defined in config.php
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result
curl_setopt($ch, CURLOPT_POST, 1); // This is a POST request
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); // Data to POST

// Grab URL and return content
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HEADER, 0); // No header in the result
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result
curl_setopt($ch, CURLOPT_POST, 1); // This is a POST request
curl_setopt($ch, CURLOPT_POSTFIELDS, array( // Data to POST
'url' => $url,
'keyword' => $keyword,
'format' => $format,
'username' => $username,
'password' => $password
));

// Fetch and return content
$data = curl_exec($ch);
curl_close($ch);

// Echo result (either a new short URL, or an error message beginning with "Error")
echo htmlentities($data);
// Do something with the result. Here, we just echo it.
echo $data;

?>
24 changes: 5 additions & 19 deletions yourls-api.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
<?php
### Require Files
define('YOURLS_API', true);
require_once( dirname(__FILE__).'/includes/config.php' );
if (defined('YOURLS_PRIVATE') && YOURLS_PRIVATE == true)
require_once 'includes/auth.php';
if ( defined('YOURLS_PRIVATE') && YOURLS_PRIVATE == true )
require_once( dirname(__FILE__).'/includes/auth.php' );


$db = yourls_db_connect();
$return = yourls_add_new_link( $_REQUEST['url'], $_REQUEST['keyword'], $db );

switch ( $_REQUEST['format'] ) {
case 'json':
header('Content-type: application/json');
echo yourls_json_encode($return);
break;

case 'xml':
header('Content-type: application/xml');
echo yourls_xml_encode($return);
break;

case 'simple':
default:
echo $return['shorturl'];
break;
}
yourls_api_output( $_REQUEST['format'], $return );

die();

0 comments on commit ce48a01

Please sign in to comment.