I am working on a Raspberry Pi Mirror that will be controlled via speech. If a set command is not found, it will search Wolfram Alpha for an answer.
The script is written in php with addaptions from other questions here, but the problem is, it won't take any inputs with spaces. I've tried both foo+bar and foo%20bar but neither will work.
The script:
<?php
$input = $_GET["input"];
$url = "http://api.wolframalpha.com/v2/query?input=" . $input . "&appid=XXXXX&format=plaintext&podindex=2";
echo $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
ob_start();
curl_exec($ch);
curl_close($ch);
$contents = ob_get_contents();
ob_end_clean();
function get($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
echo get($contents,'<plaintext>','</plaintext>');
?>
The script runs normally without spaces, for example:
- "Day" returns
1 day | 86400 seconds | 1440 minutes | 24 hours
- "Pi" returns
3.141592653589793238462643383279502884197169399375105820974
- "Time" returns
2:49:13 pm BST | Tuesday, March 29, 2016
Any help would be appreciated as I am rubbish with these kind of formatting problems :)
EDIT: Looking through the code, I don't know hy I didn't just use $contents = file_get_contents($url);
instead of that very long way round, but it still won't accept the spaces : /