0

I have this sample of API code, i'm trying to embed my form into it. This code is in php. Below is the sample API code

-----------------------------------------------------------------------------------
<?php

echo(get_sentiment_rank_text("We like sharing our service with you. We hope you enjoy it as well."));

function get_sentiment_rank_text($content) {
//get the sentiment scoring info
$result = get_sentiment($content);
$jsonarray = json_decode($result);//get results as json object
$sentiment_rank = $jsonarray->sentiment->summary->sentiment_rank;//get values from json object
return $sentiment_rank;
}

function get_sentiment($phrase) {
//create array of data to be posted

$phrase = html_entity_decode(strip_tags($phrase)); //strip any HTML tags and decode the scring
$phrase = preg_replace('/[\n\r\t]/',' ',$phrase); //replace newlines and tabs
$phrase = preg_replace('/ /',' ',$phrase); //
$phrase = preg_replace( '/\s+/', ' ', $phrase );//remove multiple white spaces
$phrase = urlencode($phrase);

$post_data['app_key'] = '[API_KEY]';
$post_data['phrase'] = $phrase;
//$post_data['callback'] = 'myFunction';
$post_data['format'] = 'json';
//$post_data['ref'] = 'ABC123';
$post_data['showoveralltext'] = 'false';
$post_data['showsummarybreakdown'] = 'false';
$post_data['showsentencetext'] = 'false';

//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//create cURL connection
$curl_connection = curl_init('http://api.sentirate.com/sentiment/request.do');

//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//perform our request
$result = curl_exec($curl_connection);

// curl_error($curl_connection);
//close the connection
curl_close($curl_connection);

return $result;//String that is a JSON String - i.e. resultant web page

}

?>

-----------------------------------------------------------------------------------------------------------------

and this is my form, is just a simple form

-----------------------------------------------------------------------------------------------------------------
</head>
<body>
<form name="Senti form" method="POST" action="">
<textarea name = "ftext" rows = "10" cols = "50"></textarea>
<br>
<input type="submit" value="send" name ="btn1">
<br>
</body>

</html>

i would be glad if anyone hear can guide me to embed the code.

Thanks

2
Contributors
12
Replies
13
Views
5 Years
Discussion Span
Last Post by vergil1983
0

normally when you post your form it should redirect to your php file.and your form should post the value of your form field into a variable in the php file.like for example

</head>
<body>
<form name="Senti form" method="POST" action="name of php file">
<textarea name = "ftext" rows = "10" cols = "50"></textarea>
<br>
<input type="submit" value="send" name ="btn1">
<br>
</body>

</html>

Php file
<?php

$textarea = $_POST['ftext'];

?>

so when you submit the html for the data that you entered in the form is store in a variable i the php file then you do what youhave to do with the data youve got stored there.

but you will have to give more details of what you are trying to accomplish here.

0

thanks eltonpiko for your explaination, this is a sentiment analysis API code i got from Sentirate. I'm trying
to embed it into a simple code to test out the sentiment value of a sentences or a paragraph of user review.
Hope that gives you more information about what i am trying to do....

0

ok so i think what you have to do is on you html form you need to post the value of the text area to the

Sentirate php api

so your htlm for text area is called

<textarea name = "ftext" rows = "10" cols = "50"></textarea>

the name of the text area is called "ftext"

now on the php page of the Sentirate api you need to chage the past phrase value to get what the user has enterd inside you text area

$post_data['app_key'] = '[API_KEY]';
$post_data['phrase'] = $phrase;

instead you change it to $post_data['ftext'] = $phrase;

try it and let me know

0

erm... it still not working. Is it something i left out???
and 1 thing, my POST method should submit which link?
here is the implementation guide of the sentiment API, http://sentirate.com/?page_id=475....
sorry that i ask too much question... but i have not much knowledge in working on an API.
and doi need a server for it??

Edited by vergil1983

0

ofcourse you need an apache server to run php code so download wamp server and install it on you computer http://www.wampserver.com/en/

you need to creat your html file that contain the form name it index.html lets say

<html>

<head>
</head>

<body>
<form name="Senti form" method="POST" action="sentirate.php">

<textarea name = "ftext" rows = "10" cols = "50"></textarea>
<br>
<input type="submit" value="send" name ="btn1">
<br>
</form>
</body>

</html>

then you create your php file that contains the api and you name it sentirate.php

<?php

echo(get_sentiment_rank_text("We like sharing our service with you. We hope you enjoy it as well."));

function get_sentiment_rank_text($content) {
//get the sentiment scoring info
$result = get_sentiment($content);
$jsonarray = json_decode($result);//get results as json object
$sentiment_rank = $jsonarray->sentiment->summary->sentiment_rank;//get values from json object
return $sentiment_rank;
}

function get_sentiment($phrase) {
//create array of data to be posted

$phrase = html_entity_decode(strip_tags($phrase)); //strip any HTML tags and decode the scring
$phrase = preg_replace('/[\n\r\t]/',' ',$phrase); //replace newlines and tabs
$phrase = preg_replace('/ /',' ',$phrase); //
$phrase = preg_replace( '/\s+/', ' ', $phrase );//remove multiple white spaces
$phrase = urlencode($phrase);

$post_data['app_key'] = '[API_KEY]';
$post_data['ftext'] = $phrase;
//$post_data['callback'] = 'myFunction';
$post_data['format'] = 'json';
//$post_data['ref'] = 'ABC123';
$post_data['showoveralltext'] = 'false';
$post_data['showsummarybreakdown'] = 'false';
$post_data['showsentencetext'] = 'false';

//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//create cURL connection
$curl_connection = curl_init('http://api.sentirate.com/sentiment/request.do');

//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//perform our request
$result = curl_exec($curl_connection);

// curl_error($curl_connection);
//close the connection
curl_close($curl_connection);

return $result;//String that is a JSON String - i.e. resultant web page

}

?>

you need to register on the sentirate and get an api key that look something like this 2c5f109bd62y0e8291fe55cce38d8fa5
and put it it this place

$post_data['app_key'] = '[API_KEY]';

then it will look like this $post_data['app_key'] = '[2c5f109bd62y0e8291fe55cce38d8fa5]';

lastly you go on wamp create a folder inside you www folder name it test or anyting place the index.html and sentirate.php file in this folder.

then go to your browser and type localhost you will see the folder called test

hope this help

Edited by eltonpiko

0

thanks again eltonpiko, i wil try it.
I'm using easyphp, i think it will work too..

0

i have try it, seems to work, but i'm not sure did my text is capture by the variable and sent to the API to test, because no matter how it just return me a "Neutral" value. is there anyway i can test it? or there is other problem?? Sorry to bother you again

0

their must be anothe problem.when you enter text in the texbox and send it what you get on the next page?
do you get some sort of error?

also i try to figure out about the api key if it should be like this

$post_data['app_key'] = '[2c5f109bd62y0e8291fe55cce38d8fa5]';

or

$post_data['2c5f109bd62y0e8291fe55cce38d8fa5] = '[API_KEY]';

try playing with this also

i like to help so its ok to ask question when you have doubts

0

thanks for your info, i manage to make it run by making changes in this 2 place

$post_data['app_key'] = '(my API key, can't tell you that. LOL)';
$post_data['phrase'] = $_POST['ftext'];

it runs and return me the desire result.
Just 1 thing is when i change the format from 'json' to 'xml', it shows me error on line 10, which is this 1:

$sentiment_rank = $jsonarray->sentiment->summary->sentiment_rank;//get values from json object

the format changing is this:
($post_data['format'] = 'json';)

i know i should change this to xml thingy. Do you have any advice for it?

Now i'm planning out the xml format to print out the result....

Edited by vergil1983

0

glad you got it working.i havent try to get it to display result in xml ill check and let you know

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.