我是网络编程的新手,我想知道如何解决这个小问题。
http://wokinfo.com/cgi-bin/dci/search.cgi
在以下网页上有一个"帖子"表格可供搜索。我可以看到以下名称=值对存在" search = university" (仅举例)但是当我尝试使用phpcurl执行此操作时,它似乎无法正常工作。这是我的简单代码。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://wokinfo.com/cgi-bin/dci/search.cgi");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"search=university");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
print curl_getinfo($ch, CURLINFO_HTTP_CODE) . "<br>";
print $output . "\n";
curl_close($ch);
我做错了什么?
答案 0 :(得分:0)
您忘记发送searchtype参数:
curl_setopt($ch, CURLOPT_POSTFIELDS, "search=university&searchtype=and");
答案 1 :(得分:-2)
您需要将CURLOPT_POSTFIELDS
的值设置为如下数组:
curl_setopt($ch, CURLOPT_POSTFIELDS, array("search" => "university"));
虽然文档声明您可以将params添加为字符串,但这似乎总是很麻烦。容易出错。传递阵列更清晰。这是在你的代码中:
// Set the `post` fields.
$post_fields = array();
$post_fields['search'] = 'university';
$post_fields['searchtype'] = 'and';
$post_fields['client'] = 'default';
$post_fields['output'] = 'xml_no_dtd';
$post_fields['proxystylesheet'] = 'default';
// Core curl logic.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://wokinfo.com/cgi-bin/dci/search.cgi");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
print curl_getinfo($ch, CURLINFO_HTTP_CODE) . "<br>";
print $output . "\n";
curl_close($ch);