duanli0162 2014-01-06 03:57
浏览 36

如何使用PHP将多个Import.io搜索引擎连接器组合到一个搜索引擎中?

I have PHP API codeblocks from a data extraction tool/website (http://import.io) in the form below. I want to have a search box that returns the result from not one, but multiple of these "connector" codeblocks (they are called connectors because they connect your search queries with the results piped through import.io, presumably).

I'm a noob at PHP, so I'm not sure how to go about this.

<?php

$userGuid = "kjnjkn-32d2-4b1c-a9c5-erferferferferf";
$apiKey = "APIKEY";

function query($connectorGuid, $input, $userGuid, $apiKey) {

  $url = "https://api.import.io/store/connector/" . $connectorGuid . "/_query?_user=" . urlencode($userGuid) . "&_apikey=" . urlencode($apiKey);

  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  curl_setopt($ch, CURLOPT_POSTFIELDS,  json_encode(array("input" => $input)));
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  $result = curl_exec($ch);
  curl_close($ch);

  return json_decode($result);
}

// Query for tile WEBSEARCH
$result = query("98c9bac2-e623-4e31-8a3e-erferferferf", array(
  "search_query" => "term1",
), $userGuid, $apiKey);
var_dump($result);

// Query for tile WEBSEARCH
$result = query("98c9bac2-e623-4e31-8a3e-bferfreferfe", array(
  "search_query" => "term2",
), $userGuid, $apiKey);
var_dump($result);
  • 写回答

1条回答 默认 最新

  • doulan9419 2014-01-31 18:16
    关注

    I think the first thing you will want is some kind of HTML form that POSTs to your PHP script. I haven't tested this but something like it will do:

    <form action="path/to/myscript.php" method="POST">
        <input type="text" name="search" placeholder="query">
        <input type="submit" value="Search">
    </form>
    

    This will issue an HTTP POST request to your script (call it myscript.php or change the HTML to match your filename) with the input term in the $_POST data array.

    This means you can get the search term typed in using $_POST["search"], and use it as the input to the query:

    $result = query("98c9bac2-e623-4e31-8a3e-erferferferf", array(
      "search_query" => $_POST["search"],
    ), $userGuid, $apiKey);
    var_dump($result);
    

    Notes:

    • There is zero validation on this - you will want to sanitize the form input if you put this in production anywhere
    • There is an interesting guide on the PHP site, which says similar things.
    • If you do anything more complex than this you will almost certainly be better to use a fully-fledged client library for a language other than PHP - there are more listed on this page.
    • Full disclosure, I work for import.io (hope this has helped!)
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程