Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用curl从外部站点获取搜索结果_Php_Curl - Fatal编程技术网

Php 使用curl从外部站点获取搜索结果

Php 使用curl从外部站点获取搜索结果,php,curl,Php,Curl,我有两个网站,一个主要,一个外部。在主站点上,我使用Lucene进行搜索。问题是,我还试图通过外部网站进行搜索 外部站点的表单操作: <form action="https://secure.bcchf.ca/SuperheroPages/searchResults.cfm?Event=WOT" method="post" name="search_tribute" > 我尝试使用curl,但它只显示搜索表单,而没有实际执行搜索(字段也是空的) 有什么建议吗 我无法访问表单

我有两个网站,一个主要,一个外部。在主站点上,我使用Lucene进行搜索。问题是,我还试图通过外部网站进行搜索

外部站点的表单操作:

<form action="https://secure.bcchf.ca/SuperheroPages/searchResults.cfm?Event=WOT" method="post" name="search_tribute" >

我尝试使用curl,但它只显示搜索表单,而没有实际执行搜索(字段也是空的)


有什么建议吗


我无法访问表单操作,因为它位于外部站点上。我所拥有的只是一个表单,当我提交它时可以链接到它。

试着把它放到一个数组中。 因为这将是变量$\u在另一端进行POST检查

刚刚检查了你的链接,它是该领域的团队名称

$fields = array("teamName"=>"julia");
然后

curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
所以你的完整代码是

<?php
 $ch = curl_init("https://secure.bcchf.ca/SuperheroPages/searchResults.cfm?Event=WOT");
 $fields = array("teamName"=>"julia");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_POST, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
 $output = curl_exec($ch);
 var_dump($output);
 curl_close($ch);
 ?>

大多数搜索引擎使用
GET
而不是
POST
。。你可以试试

// asumption
$_POST['search'] = "hello";

// Return goole Search Result
echo curlGoogle($_POST['search']);

function curlGoogle($keyword) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/search?hl=en&q=' . urlencode($keyword) . '&btnG=Google+Search&meta=');
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FILETIME, true);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
或者如果你想发帖的话

curl_setopt($ch, CURLOPT_POSTFIELDS, array("search"=>"hello"));

您的php代码语法无效,无法编译

因此,如果这确实是您的问题所在,那么您的文件将生成一个致命错误

也就是说,这个问题很难回答,因为我们不知道你想要从哪个网站获取搜索结果

尝试如下修改您的行:

curl_setopt($ch, CURLOPT_POSTFIELDS, "search=hello");
或者

curl_setopt($ch, CURLOPT_POSTFIELDS, array("search" => "hello");
也许它可以工作,但是可能需要更多的post数据,或者元素名称不正确

您必须查看表单,或者尝试提出请求,然后使用chromes developer工具或firebug查看表单

外部网站也有很多方法来阻止你所做的事情,尽管一切都可以通过某种方式解决

假设情况并非如此,我希望我能帮助您。


<?php
$ch = curl_init("https://secure.bcchf.ca/SuperheroPages/searchResults.cfm?Event=WOT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("teamName" => "hello", "searchType" => "team"));
$output = curl_exec($ch);
echo $output;
curl_close($ch);
?>
你能试试这个吗?
我很确定它应该是teamName而不是tName

您可能需要在帖子中包含更多字段,而且search='hello'似乎不是有效的语法。根据站点的不同,您可能还需要设置引用者。搜索论坛不是表单元素的名称实际上只有一个输入字段,名为“tname”。当我添加URL时,它会进入搜索结果页面,但仍然没有搜索任何内容。此外,表单显示不完整。所以您尝试在数组中传递'tname',就像我现在所做的那样?是的,我已将搜索更改为tname。您绝对肯定这是一个POST而不是GET?表单仅使用POST在本例中是ANKS,我已尝试将其放入数组中,但是搜索页面仍然是空白的。在这种情况下,您必须在请求搜索结果时进一步检查发送的post请求。但是我无法真正检查它,因为我无法访问外部网站以及它如何解析搜索输入“您必须查看表单或尝试提出请求,然后使用chromes developer tools或firebug查看表单。“谢谢,但问题和以前一样。它进入搜索结果页面,但仍然没有搜索到任何内容。表单也消失了。是的,我知道它是teamName,但我在这里发布时有点匆忙。天哪,你刚刚添加了搜索类型吗?非常感谢。是的,我只添加了这些。没问题!
<?php
$ch = curl_init("https://secure.bcchf.ca/SuperheroPages/searchResults.cfm?Event=WOT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("teamName" => "hello", "searchType" => "team"));
$output = curl_exec($ch);
echo $output;
curl_close($ch);
?>