| 13532 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Application model for CakePHP.
|
|
|
4 |
*
|
|
|
5 |
* This file is application-wide model file. You can put all
|
|
|
6 |
* application-wide model-related methods here.
|
|
|
7 |
*
|
|
|
8 |
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
9 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
10 |
*
|
|
|
11 |
* Licensed under The MIT License
|
|
|
12 |
* For full copyright and license information, please see the LICENSE.txt
|
|
|
13 |
* Redistributions of files must retain the above copyright notice.
|
|
|
14 |
*
|
|
|
15 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
16 |
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
17 |
* @package app.Model
|
|
|
18 |
* @since CakePHP(tm) v 0.2.9
|
|
|
19 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
App::uses('Model', 'Model');
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Application model for Cake.
|
|
|
26 |
*
|
|
|
27 |
* Add your application-wide methods in the class below, your models
|
|
|
28 |
* will inherit them.
|
|
|
29 |
*
|
|
|
30 |
* @package app.Model
|
|
|
31 |
*/
|
|
|
32 |
class AppModel extends Model {
|
|
|
33 |
|
|
|
34 |
public function make_request($url,$fields,$format='json'){
|
|
|
35 |
$fields_string = '';
|
|
|
36 |
if(!empty($fields)){
|
|
|
37 |
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
|
|
|
38 |
rtrim($fields_string, '&');
|
|
|
39 |
}
|
|
|
40 |
//open connection
|
|
|
41 |
$ch = curl_init();
|
|
|
42 |
|
|
|
43 |
//set the url, number of POST vars, POST data
|
|
|
44 |
curl_setopt($ch,CURLOPT_URL, $url);
|
|
|
45 |
curl_setopt($ch,CURLOPT_RETURNTRANSFER , true);
|
|
|
46 |
if(!empty($fields)) {
|
|
|
47 |
curl_setopt($ch,CURLOPT_POST, count($fields));
|
|
|
48 |
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
|
|
|
49 |
}
|
|
|
50 |
//execute post
|
|
|
51 |
$result = curl_exec($ch);
|
|
|
52 |
//close connection
|
|
|
53 |
curl_close($ch);
|
|
|
54 |
switch($format){
|
|
|
55 |
case 'json':
|
|
|
56 |
$response = json_decode($result,1);
|
|
|
57 |
break;
|
|
|
58 |
}
|
|
|
59 |
return $response;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
public function make_bitly_url($url,$login,$appkey,$format = 'xml',$version = '2.0.1') {
|
|
|
63 |
//create the URL
|
|
|
64 |
$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;
|
|
|
65 |
|
|
|
66 |
//get the url
|
|
|
67 |
//could also use cURL here
|
|
|
68 |
$response = file_get_contents($bitly);
|
|
|
69 |
|
|
|
70 |
//parse depending on desired format
|
|
|
71 |
if(strtolower($format) == 'json')
|
|
|
72 |
{
|
|
|
73 |
$json = @json_decode($response,true);
|
|
|
74 |
return $json['results'][$url]['shortUrl'];
|
|
|
75 |
}
|
|
|
76 |
else //xml
|
|
|
77 |
{
|
|
|
78 |
$xml = simplexml_load_string($response);
|
|
|
79 |
return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
}
|