用于搜索集成在javascript代码段中的论坛的PHP代码
问题描述:
I made a chrome extension to search the website dev.bukkit.org, and it works great with this code:
<script>
function onLoad() {
document.getElementById("mytextfield").focus();
}
function onKeyPress(e) {
if (e.keyCode == 13) {
openResults();
}
}
function openHomePage() {
window.open("http://dev.bukkit.org/");
}
function openResults() {
window.open("http://dev.bukkit.org/search/?search=" + encodeURIComponent(document.getElementById("mytextfield").value));
}
</script>
</head>
<body onload="onLoad();">
<img src="png-3.png" onclick="openHomePage();" style="border-width: 0px; cursor: pointer" /><br>
<div name="myFormDiv" style="center: 6px;">
<br>
<center><input type="search" id="mytextfield" name="mytextfield" placeholder="Search..." onkeypress="onKeyPress(event);" /></center>
</div>
I now would like to make an option to search forum.bukkit.org. Unfortunately, it requires a token to search the forum. A friend gave me a php snippet that lets you search the forum, but I am having trouble integrating it into the original code to search dev.bukkit.org. Help would be appreciated, I am lost! PHP Forum Search code:
<?php
$q=$_GET['q'];
echo do_post_request("http://forums.bukkit.org/search/search",'keywords='.$q.'&_xfToken=10000%2C1333276150%2C1b8a644c97b33e9cfda0e15170ca5185cf15bc3a');
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
?>
Thank you:)
答
Based on Adding custom HTTP headers using JavaScript, you should use XmlHttpRequest
. My personal suggestion would be to use ajax to call php script and retrieve data. You can implement it easily with jQuery's .post().