forked from luqmaan/green-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
34 lines (23 loc) · 816 Bytes
/
api.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
<?php
header('content-type: application/json; charset=utf-8');
$q = $_GET['q'];
$id = $_GET['id'];
$url = "http://api.goodguide.com/search.xml?api_key=xghpskqxrn6u6pdpxr83aznu&api_version=1.0&api_format=simple";
$req = $url . "&q=" . $q;
if (isset($id))
$req = $url . "&id=" . $id;
$ch = curl_init($req);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
$output = curl_exec($ch);
$xml = simplexml_load_string($output);
$json = json_encode($xml);
echo isset($_GET['callback'])
? "{$_GET['callback']}($json)"
: $json;
?>