-
Notifications
You must be signed in to change notification settings - Fork 3
/
urigen.php
87 lines (71 loc) · 1.84 KB
/
urigen.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
// Image URI Generator
// Copyright 2012, Senthil Padmanabhan
// Released under the MIT License
// http://www.opensource.org/licenses/MIT
$query = array(); // query list
$response = array(); // response object
// Get query params
$params = $_GET['params'];
if(!$params) {
$params = $_POST['params'];
}
// Validate input
if($params) {
$query = json_decode(stripslashes($params), true);
$query = $query? $query['images']: 0;
}
// Validate input data
if($query && count($query)) {
$data = array();
foreach ($query as $url) {
$url = "http://".preg_replace('/http:\/\//i', '',$url);
$respObj = array('url' => $url);
// Defining the default CURL options
$defaults = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => TRUE
);
// TODO make these curl requests in parallel using pluton or other libraries
// Open the Curl session
$session = curl_init();
// Setting the options
curl_setopt_array($session, $defaults);
// Make the call
$imgResp = curl_exec($session);
// Handle response
if($imgResp) {
$respObj['uri'] = base64_encode($imgResp);
} else {
$respObj['error'] = getErrorResp(101, curl_error($session));
}
$data[] = $respObj;
// Close the connetion
curl_close($session);
}
$response['data'] = $data;
} else {
// No input - build error response and return
$response['error'] = getErrorResp(100, 'No input URLs');
}
// Set HTTP header to JSON
// TODO also add cahce headers if necessary
header("Content-Type: application/json");
// echo out the JSON
echo json_encode($response);
/**
* Utility Functions
*/
/**
*
* Function to build the error response
*
* @function getErrorResp
* @param $id {String} Error ID
* @param $msg {String} Error Message
*
*/
function getErrorResp($id, $msg) {
return array('id' => $id, 'msg' => $msg);
}
?>