In this article, I will show how you can use the Anonymous Question
Formspring service to organize the Question / Answer section on a third-party resource in PHP.
Formpring API
exists in nature (REST-like), but now it is at the “beta” stage, and only favorites can use it, as far as I understand. On the developer page they write: "The API is currently limited to a select group of users and partners right now." Registration is needed, which does not immediately spit out the keys necessary for the work. I already sent my application some time ago, but I never received a reply. However, some API methods can be used without authentication (a banal receipt of information).
Now I will show how I personally bypassed the problems. As I noted in the title, this is the simplest service that can:
- Ask anonymous questions;
- Display a list of answers.
To work with the service, we naturally need an account on Formspring. Let's start with the simple and obvious: output answers from the account.
Displaying a list of answers
There are several ways: to parse the RSS feed of the account or use the API. Guess which option we choose? Right, the second.
To do this, there is a method / answered / list, which will display the last 20 entries on the page. In addition, there are additional parameters for the query:
- max_id - returns records with id <max_id
- since_id - returns records with id> since_id
- before - same as max_id
At the same time max_id and since_id cannot be used simultaneously.
')
For our service, we limit ourselves to the standard issue of 20 entries. There are no difficulties here, so
let's use the function
file_get_contents () . We make a request by URL:
beta-api.formspring.me/answered/list _
Such a request will return us a list of responses in JSON format.
As a result, we get something like this:
$data = json_decode(file_get_contents('http://beta-api.formspring.me/answered/list/_')); foreach ($data->response as $item) { echo ' <strong>'.$item->question.'</strong> ('.date('d.m', strtotime($item->time)).' <a href="http://www.formspring.me/_/q/'.$item->id.'" target="_blank">#</a>) <br /> '.$item->answer.' <hr /> '; }
Note: another entry contains the field asked_by, which will not be empty if the question is not anonymous.
Sending an anonymous question
Here begins the main shamanism. First of all, we get a token that will allow us to successfully implement our plans. For this, it is best to log out (log out), i.e. become anonymous and go to your page:
www.formspring.me _
We look at the source code of the page (the keys Ctrl + U are usually used). We are looking for either “token” or “id =” ask ”, inside which we are looking for a hidden token field.
Now you need to know the User-Agent of your browser. According to my observations, the token is generated using the value of the User-Agent, so you need to know these values ​​for the browser in which you searched for token. This can be done in different ways. For example, for Firefox or Google Chrome, you can open the JavaScript console and type:
navigator.userAgent
For example, for my browser, this is the line:
Mozilla / 5.0 (Windows NT 6.1; WOW64; rv: 12.0) Firefox / 12.0 Gecko / 20100101
Now go directly to the coding. Create a form with a field to enter the question text. The form submission handler should be approximately as follows:
$ch = curl_init('http://www.formspring.me/profile/ask/_'); $data = array( 'token' => _, 'question' => __, 'ajax' => 1 ); curl_setopt($ch, CURLOPT_URL, 'http://www.formspring.me/profile/ask/_'); curl_setopt($ch, CURLOPT_REFERER, 'http://www.formspring.me/_'); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($ch, CURLOPT_USERAGENT, _USER-AGENT); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if (curl_exec($ch) !== FALSE) { echo ' !'; } else { echo curl_error($ch); } curl_close($ch);
We use the cURL library to simulate the browser and send the form to the Formspring server.
If everything was done correctly, it will be possible to answer the question from the profile page on Formspring (like other question / answer management). I didn’t describe everything in detail, didn’t consider error handling, concentrating only on reporting the main idea.
An example of the script is also attached.
UPDATE:By numerous requests I
filled in source codes and examples of use on github. Designed everything in a separate class. Probably, there is a lot of India in the code, so report any errors, inaccuracies and complaints!
UPDATE 2:Also, people asked for the source of the page itself.
Get it .