forked from YOURLS/YOURLS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improving the API functions and API sample file to reflect the new au…
…th system git-svn-id: http://yourls.googlecode.com/svn/trunk@22 12232710-3e20-11de-b438-597f59cd7555
- Loading branch information
Showing
2 changed files
with
34 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |