The simplest Q & A service using Formspring
The Formpring API exists in nature (REST-like), but now it is at the “beta” stage, and, as I understand it, only a select few can use it fully. On the page for developers write: "The API is currently limited to a select group of users and partners right now." Need registration, which does not immediately spit out the keys necessary for work. I already sent my application some time ago, but did not receive a response. However, some API methods can be used without authentication (banal receipt of information).
Now I will show how personally I circumvented the problems that arose. As I noted in the title, this is the simplest service that can:
- Ask anonymously questions;
- List answers.
To work with the service, we naturally need an account on Formspring. Let's start with the simple and obvious: displaying responses from the account.
List response
There are several ways: parse the RSS feed of your account or use the API. Guess which option we choose? Right, second.
There is a / answered / list method for this, which will display the last 20 entries on the page. In addition, there are additional parameters for the request:
- 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 restrict ourselves to the standard output of 20 entries. There are no difficulties here, so we will use the file_get_contents () function . We make the request by URL: Such a request will return us a list of responses in JSON format. As a result, we get something like this code:
beta-api.formspring.me/answered/listВАШ_ЛОГИН$data = json_decode(file_get_contents('http://beta-api.formspring.me/answered/list/ВАШ_ЛОГИН'));
foreach ($data->response as $item)
{
echo '
'.$item->question.'
('.date('d.m', strtotime($item->time)).' id.'" target="_blank">#)
'.$item->answer.'
';
}
Note: the entry also contains the asked_by field, which will not be empty if the question was asked not anonymously.
Submit an anonymous question
This is where the main shamanism begins. First of all, we get a token, which will allow us to successfully implement our plan. For this, it is best to log out (log out), i.e. Become an anonymous and go to your page: We look at the source code of the page (Ctrl + U keys are usually used). We are looking there for either “token” or “id =” ask ””, inside which we are looking for a hidden token field. Now you need to find out the User-Agent of your browser. According to my observations, token is generated using the User-Agent value, so you need to find out these values for the browser in which you searched for token. You can do this in many ways. For example, for Firefox or Google Chrome, you can open the JavaScript console and enter: For example, for my browser this is the line:
www.formspring.meВАШ_ЛОГИНnavigator.userAgentMozilla / 5.0 (Windows NT 6.1; WOW64; rv: 12.0) Gecko / 20100101 Firefox / 12.0
Now we pass directly to the coding. Create a form with a field for entering the question text. The form submission handler should look something like this:
$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 operation of the browser and submit the form to the Formspring server.
If everything was done correctly, you can answer the question from the profile page on Formspring (as well as other question / answer management). I did not describe everything in detail, I did not consider error handling, concentrating only on conveying the main idea.
An example of the script is also included.
UPDATE:
By popular demand, I uploaded the sources and examples of use on github. Designed everything in a separate class. There are probably a lot of India in the code, so report bugs, inaccuracies and complaints!
UPDATE 2:
Also people asked for the source of the page itself. Get it .