【求助】php使用curl模拟post提交数据获取目标网站信息,求解?

WBOY
Release: 2016-06-06 20:15:11
Original
1117 people have browsed it

代码如下,但是没有获取到搜索这个$name关键词应有的数据,求解?

<code>$name="进击的巨人";
$url="http://www.4567.tv/search.asp";
$data = array (
    'typeid'=>'2',
    'keyword' => urlencode(mb_convert_encoding($name, "GBK","UTF-8")),
);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1 );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $c=curl_exec($ch);
    curl_close($ch);</code>
Copy after login
Copy after login

回复内容:

代码如下,但是没有获取到搜索这个$name关键词应有的数据,求解?

<code>$name="进击的巨人";
$url="http://www.4567.tv/search.asp";
$data = array (
    'typeid'=>'2',
    'keyword' => urlencode(mb_convert_encoding($name, "GBK","UTF-8")),
);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1 );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $c=curl_exec($ch);
    curl_close($ch);</code>
Copy after login
Copy after login

首先你必须根据抓包数据来确定POST的数据。假设你的POST方式和数据是没有问题的,我只是根据你的代码说出目前的问题。

修改后的代码:

<code>$name="进击的巨人";
$url="http://www.4567.tv/search.asp";
$data = array (
    'typeid'=>'2',
    'keyword' => mb_convert_encoding($name, "GBK","UTF-8"), //注意这里
);

$data = http_build_query($data);//注意这里
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$c=curl_exec($ch);
curl_close($ch);</code>
Copy after login

如果抓包正确,上述代码不能正确执行的话,请根据具体的问题解决,通常可以尝试添加Reffer/UserAgent等信息。

urlencode 去掉

这是接口的请求例子吧?
应该没有什么问题,你把根证书验证关了试一下

<code>curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template