我有一个带搜索表单的html文件:
<form id="twit" method="post">
<div class="col-md-6">
<input type="text" name="venue" class="search-query form-control" placeholder="Venue Name" />
</div>
<div class="col-md-6">
<input type="text" name ="location" class="search-query form-control" placeholder="Venue City" />
</div>
</div>
<span class="input-group-btn">
<button type="submit" class="btn btn-danger" type="button">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</form>
使用此搜索表单尝试向twitter搜索API提交两个查询参数。
我的php文件是这样的:
$url = "https://api.twitter.com/1.1/search/tweets.json";
$requestMethod = "GET";
//if ($_SERVER["REQUEST_METHOD"] == "GET") {
//if(isset($_POST['venue']) && !empty($_POST['location'])){
$venue = $_GET['venue'];
$location = $_GET['location'];
$getfield = "?q=$venue+$location&lang=en";
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
//if($string["errors"][0]["message"] != "") {echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>".$string[errors][0]["message"]."</em></p>";exit();}
foreach($string['statuses'] as $items)
{
//echo "Tweet: ". $items['text']."<br />";
// Create map with request parameters
$params = array ('txt' => $items['text']);
// Build Http query using params
$query = http_build_query ($params);
// Create Http context details
$contextData = array (
'method' => 'POST',
'header' => "Connection: close\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: ".strlen($query)."\r\n",
'content'=> $query );
// Create context resource for our request
$context = stream_context_create (array ( 'http' => $contextData ));
// Read page rendered as result of your POST request
$result = file_get_contents (
'http://sentiment.vivekn.com/api/text/', // page url
false,
$context);
// Server response is now stored in $result variable so you can process
print_r($result."<br />");
}
//} //else {
//echo "<h3 id='submit-error'>Sorry, there was a problem.</h3><p>Make sure you filled in all fields!</p><p><em>";
//}
//}
当我转到这样的网址时http://twitter.somewebsite.com/twitter.php?venue=test&location=washington我从情绪引擎中得到以下信息(我需要一段时间才知道如何加快速度?Api有这个但不知道我怎么能加入它。已添加批处理API,因为大多数人都会遇到网络延迟。要使用它,请发送一个POST请求,其中包含要归类为http://sentiment.vivekn.com/api/batch/的文本的JSON数组。您将收到另一个JSON与请求中的顺序相对应的响应数组(类似于上面的一个)。每个请求的最大内容大小可以是1 MB
使用onclick功能我试试这个但没有任何反应:
$.getJSON( "twitter.php", { venue: "ikea", location: "amsterdam" } )
.done(function() {
alert( "success" );
})
.fail(function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
})
.always(function() {
alert( "complete" );
});
我对PHp几乎没有经验,几乎没有使用ajax的经验,所以提前感谢。 所以我的问题是:
答案 0 :(得分:0)
使用以下内容:
HTML:
<form id="twit">
<div class="col-md-6">
<input type="text" name="venue" class="search-query form-control" placeholder="Venue Name" />
</div>
<div class="col-md-6">
<input type="text" name ="location" class="search-query form-control" placeholder="Venue City" />
</div>
</div>
<span class="input-group-btn">
<button class="btn btn-danger" type="button">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</form>
PHP:
$texts = array();
foreach($string['statuses'] as $items)
{
$texts[] = $items['text'];
}
$json_texts = json_encode($texts);
$ch = curl_init('http://sentiment.vivekn.com/api/batch/');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_texts);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($json_texts))
);
$result = curl_exec($ch);
AJAX:
$.ajax({
type: "GET",
url: "twitter.php",
data: data,
success: function(msg){
}
});