dongmi5015 2012-04-27 04:38
浏览 44
已采纳

用于搜索集成在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:)

  • 写回答

1条回答 默认 最新

  • douyun3799 2012-04-27 06:01
    关注

    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().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型