Jump to content

Help understand how to use API and display it on my site


Go to solution Solved by mentalist,

Recommended Posts

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

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

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

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

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...

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)
?>

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 ;)

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

  • Solution

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;
?>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.