简体   繁体   English

使用curl带来来自外部站点的搜索结果

[英]Using curl to bring search results from external site

I have 2 sites, one main, one external. 我有2个站点,一个主要站点,一个外部站点。 On the main site, I am using Lucene to search through it. 在主站点上,我正在使用Lucene进行搜索。 The problem is, I am trying to also search through the external site. 问题是,我正在尝试通过外部站点进行搜索。

The Form action for the external site: 外部站点的表单操作:

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

I've tried to use curl, but it only brings up the search form without actually doing the search (the field is empty as well). 我尝试使用curl,但是它只显示搜索表单而没有实际执行搜索(该字段也为空)。

<?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, tname='hello');
$output = curl_exec($ch);
echo $output;
curl_close($ch);
?>

Any tips? 有小费吗?

I don't have access to the form action since it's on an external site. 我无法访问表单操作,因为它位于外部站点上。 All i have is a form that links to it when I submit it. 我所拥有的只是我提交时链接到它的表格。

<?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);
?>

Can you try this? 你可以试试这个吗? I'm pretty sure it's supposed to be teamName instead of tName 我很确定它应该是teamName而不是tName

Try just putting it into an array. 尝试将其放入数组中。 as that will be the variable the $_POST checks on the other side 因为那将是变量$ _POST在另一侧检查

and just checked your link, its teamName for the field 并检查了您的链接,该字段的teamName

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

Then.. 然后..

curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

So your complete code is... 因此,您的完整代码是...

<?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);
 ?>

Most search engine use GET and not POST .. you can try 大多数搜索引擎使用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;
}

Or if you want post then 或者,如果您要发布,那么

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

Your php code is not valid syntax, it does not compile. 您的php代码语法无效,无法编译。

So if this is really what you have, your problem is that your file generates a fatal error. 因此,如果这确实是您的全部,那么您的问题是您的文件生成了致命错误。

That being said, this question is hard to answer since we don't know the site you want to grab your search results from. 话虽如此,这个问题很难回答,因为我们不知道您想从中获取搜索结果的网站。

Try modifying your line like this: 尝试像这样修改您的行:

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

or alternatively 或者

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

Maby it will work, however it may be that more post data is required or that the element name is not correct. Maby可以运行,但是可能需要更多的发布数据,或者元素名称不正确。

You have to look at the form or try making a request and look at it with chromes developer tools or firebug. 您必须查看该表格或尝试发出请求,然后使用chromes开发人员工具或firebug查看它。

Also there are a number of ways for external sites to prevent what you are doing, altough evertything can be worked around somehow. 外部站点也可以通过多种方式来阻止您的操作,尽管如此,一切都可以解决。

Assuming that is not the case, I hope i could help you. 假设情况并非如此,我希望我能为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM