SennNathan Posted October 24, 2013 Share Posted October 24, 2013 I know basic php and html and i want to add wolframalpha.com API to my site this is the link to where you can request the api and see what kind of format its in. http://products.wolframalpha.com/api/explorer.html I need a simple code that will request the api and display the results Thanks for any help i could really use it right now Quote Link to comment Share on other sites More sharing options...
gristoi Posted October 25, 2013 Share Posted October 25, 2013 Read the documentation: http://products.wolframalpha.com/developers/ Quote Link to comment Share on other sites More sharing options...
SennNathan Posted October 26, 2013 Author Share Posted October 26, 2013 Ok i read the doc's and they didnt really go over how to display the results on my site only how to request the results then i found http://products.wolframalpha.com/api/libraries.html and it has a sample php code but when i uploaded it to my server it didn't work i changed my api id and read the docs that came with it still no luck Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 26, 2013 Share Posted October 26, 2013 From a quick search I found this thread which gives an example: http://community.wolframalpha.com/viewtopic.php?f=31&t=78407 Then from a quick scan of the API docs, i'm guessing its set up around a simple http request using GET values and returns some kind of xml...??? * I saw a mention of JSON but nothing looked like it so... ** In principle they state that they use REST architecture (basically http requests returning xml (or similar)) http://products.wolframalpha.com/api/documentation.html Quote Link to comment Share on other sites More sharing options...
SennNathan Posted October 26, 2013 Author Share Posted October 26, 2013 Ive read all this but im yet to find an example of how to display the returned xml data to my site Quote Link to comment Share on other sites More sharing options...
SennNathan Posted October 26, 2013 Author Share Posted October 26, 2013 I also dont understand why there php example didn't work when i uploaded it to my site http://products.wolframalpha.com/api/libraries.html Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 26, 2013 Share Posted October 26, 2013 Ive read all this but im yet to find an example of how to display the returned xml data to my site XML is pretty standard stuff and there's many parsers out there, here's where you might start: http://php.net/manual/en/simplexml.examples-basic.php Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 26, 2013 Share Posted October 26, 2013 I also dont understand why there php example didn't work when i uploaded it to my site http://products.wolframalpha.com/api/libraries.html What does "didn't work" mean? Were there errors, warnings, if so did they say where from? i.e. did the server say forbidden (maybe file permissions), or php throw an error, do you have php show errors on? etc, etc... Quote Link to comment Share on other sites More sharing options...
SennNathan Posted October 26, 2013 Author Share Posted October 26, 2013 No it dont do anything when i try and search Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 26, 2013 Share Posted October 26, 2013 No it dont do anything when i try and search I haven't dl'd this api so you're going to have to show some code... Quote Link to comment Share on other sites More sharing options...
SennNathan Posted October 26, 2013 Author Share Posted October 26, 2013 Ok you can see them all here http://products.wolframalpha.com/api/libraries.html Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 26, 2013 Share Posted October 26, 2013 Ok you can see them all here http://products.wolframalpha.com/api/libraries.html Yeah I knew that! Quote Link to comment Share on other sites More sharing options...
SennNathan Posted October 26, 2013 Author Share Posted October 26, 2013 I know this is probably super weak but im still new to all this <?php $request = 'http://api.wolframalpha.com/v2/query?appid=YKAERX-QJJAE7L2G6&input=albert%20einstein&format=html'; $response = file_get_contents($request); $xml = simplexml_load_file('$response'); print_r($xml) ?> Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 26, 2013 Share Posted October 26, 2013 Right, you need to set for errors/warnings, see below: <?php error_reporting(E_ALL); ini_set('display_errors',E_ALL); //$request = 'http://api.wolframalpha.com/v2/query?appid=YKWERR-FJJAE7L2L6&input=albert%20einstein&format=html'; $request = "http://api.wolframalpha.com/v2/query?appid=YKWERR-FJJAE7L2L6&input=albert einstein"; //$request = "http://api.wolframalpha.com/v2/query?appid=YKWERR-FJJAE7L2L6&input=albert einstein&format=xml"; /* $response = file_get_contents($request, FALSE); echo $response; //$xml = simplexml_load_file('$response'); //print_r($xml) */ $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_URL, $request); $response = curl_exec($curl); curl_close($curl); //$xml = simplexml_load_file($response); //print_r($xml) echo $response; ?> Right, when I used file_get_contents() I got errors, lots of questions on it out there, so I quickly tried using curl instead and it works... BUT I then ran it through simple xml parser and it kept dumping out, I believe at the <img /> tag... BTW i've amended your id in this post Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 26, 2013 Share Posted October 26, 2013 Not being satisfied... <?php error_reporting(E_ALL); ini_set('display_errors',E_ALL); //$request = 'http://api.wolframalpha.com/v2/query?appid=YKAERX-QJJAE7L2G6&input=albert%20einstein&format=html'; $request = "http://api.wolframalpha.com/v2/query?appid=YKAERX-QJJAE7L2G6&input=albert einstein"; //$request = "http://api.wolframalpha.com/v2/query?appid=YKAERX-QJJAE7L2G6&input=albert einstein&format=MathML"; //$request = "http://api.wolframalpha.com/v2/query?appid=YKAERX-QJJAE7L2G6&input=albert einstein&format=Plaintext"; $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_URL, $request); $response = curl_exec($curl); curl_close($curl); $xml = new SimpleXMLElement($response); // <-- NOTICE DIFFERENCE HERE var_dump($xml->pod->subpod->img); ?> * Adding no format defaults to Plaintext http://products.wolframalpha.com/api/documentation.html#3 Quote Link to comment Share on other sites More sharing options...
SennNathan Posted October 26, 2013 Author Share Posted October 26, 2013 Ok this is defiantly getting better now i just need to request the html format and display it so it looks like it does on their site thanks for your help this is a big help. Quote Link to comment Share on other sites More sharing options...
Solution mentalist Posted October 26, 2013 Solution Share Posted October 26, 2013 That's already there... <?php $request = 'http://api.wolframalpha.com/v2/query?appid=YKWERR-FJJAE7L2L6&input=albert%20einstein&format=html'; $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_URL, $request); $response = curl_exec($curl); curl_close($curl); echo $response; ?> Quote Link to comment Share on other sites More sharing options...
SennNathan Posted October 26, 2013 Author Share Posted October 26, 2013 Ok i figured it out here is my link http://yousearch.mobi/wolfram.php now last thing is there anyway to cut out were it has the ]]> . Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 26, 2013 Share Posted October 26, 2013 Use regex (regular expressions) in some way, I believe there's a specific forum for it here... (or go through the xml and only display the bits you really want!) Quote Link to comment Share on other sites More sharing options...
SennNathan Posted October 26, 2013 Author Share Posted October 26, 2013 Thanks for your help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.