怎样快速获取以get或post的方式请求远程网页的内容

编辑:管理员 发布时间:2022-06-13 17:51:01477

这里以php代码为例,以get或post的方式访问远程页面或站点的页面内容分为以下几种常用的方法.

假设需要请求的页面地址为xxx.xxxx.xxx,在php代码中常用的请求方式有哪些.

<?php

$zdurl='https://网址';

?>

一、get方式请求指定页面

1: 使用php自带的函数file_get_contents(),直接使用它可以以get的方式得到页面的内容.

示例代码:

<?php

$fanhuistr = file_get_contents($zdurl);

echo $fanhuistr;

?>

2: fopen方式直接模拟打开指定链接,然后通过get方式获取页面

示例代码:

<?php

$qingqiu = fopen($zdurl, 'r');

stream_get_meta_data($qingqiu);

while(!feof($qingqiu)) {

$nr .= fgets($qingqiu, 1024);

}

echo $nr;

fclose($qingqiu);

?>

首先使用fopen()函数打开url,然后stream_get_meta_data可获得网页的meta信息,feof判断是否到文件的结尾,如果没有则feof获得页面中1024字节数的一行的信息,通果$nr叠加赋值得到最终的页面内容,最后fclose关闭请求.

3:用php的fsockopen函数来请求打开url,然后通过get方式就可以获取请求内容,以及查询端口状态等,需要注意的是fsockopen要 开启php.ini配置中allow_url_fopen选项.

<?php

$qqq = array(

'foo'=>'bar',

'baz'=>'boom',

//'site'=>'xxx.xxx.xxx',

'name'=>'xxxxxx');

$qingqiuhead = http_build_query($qqq);

$urlparse = parse_url($zdurl);

$fpnr = fsockopen($urlparse["host"], 80, $errno, $errstr, 3);

$newhead = "GET ".$urlparse['path']."?".$qingqiuhead." HTTP/1.0\r\n";

$newhead .= "Host: ".$urlparse['host']."\r\n";

$newhead .= "\r\n";

$write = fputs($fpnr, $newhead);

while (!feof($fpnr))

{

$oneline = fread($fpnr,4096);

echo $oneline;

}

?>

4:使用php中的扩展curl库,在使用curl库之前需要开启php.ini配置文件中的curl扩展.

<?php

$curl  =  curl_init ( ) ;

curl_setopt ( $curl , CURLOPT_URL ,  $zdurl ) ;

curl_setopt ( $curl , CURLOPT_HEADER ,  1 ) ;

curl_setopt ( $curl , CURLOPT_RETURNTRANSFER ,  1 ) ;

$nr  =  curl_exec ( $curl ) ;

curl_close ( $curl ) ;

print_r ( $nr ) ;

?>

首先curl_init初始化url,curl_setopt设置,CURLOPT_URL设置抓取的url,CURLOPT_HEADER设置头文件的信息为数据流,CURLOPT_RETURNTRANSFER设置获取的内容以文件流形式返回,不直接输出,curl_exec执行curl命令,curl_close关闭请求.

如果仅仅想要获取指定网页的内容,无需复杂的代码,只需要使用本站的网页内容提取工具即可.

二、以post方式请求指定的页面

1:file_get_contents函数

示例代码:

<?php

$post = array (‘foo' => ‘bar');

$post = http_build_query($post);

$newcreate = array (

'http' => array (

'method' => 'POST',

'header'=> "Content-type: application/x-www-form-urlencodedrn" .

"Content-Length: " . strlen($post) . "rn",

'content' => post

)

);

$xxx = stream_context_create($newcreate);

$nr = file_get_contents($zdurl, false, $xxx);

echo $nr;

?>

2:用fsockopen函数

<?php

$query="请求内容";

$zdurlinfo = parse_url($zdurl);

$fp = fsockopen($zdurlinfo["host"], 80, $errno, $errstr, 3);

$newdhead = "POST ".$zdurlinfo['path']."?".$zdurlinfo["query"]." HTTP/1.0\r\n";

$newdhead .= "Host: ".$zdurlinfo['host']."\r\n";

$newdhead .= "Referer: http://".$zdurlinfo['host'].$zdurlinfo['path']."\r\n";

$newdhead .= "Content-type: application/x-www-form-urlencoded\r\n";

$newdhead .= "Content-Length: ".strlen(trim($query))."\r\n";

$newdhead .= "\r\n";

$newdhead .= trim($query);

$write = fputs($fp, $newdhead);

while (!feof($fp))

{

$oneline = fread($fp,4096);

echo $oneline;

}

?>

3:php中的curl扩展来发送post请求.

<?php

$cshnr  =  curl_init ( ) ;

curl_setopt ( $cshnr , CURLOPT_URL ,  $zdurl ) ;

curl_setopt ( $cshnr , CURLOPT_HEADER ,  1 ) ;

curl_setopt ( $cshnr , CURLOPT_RETURNTRANSFER ,  1 ) ;

curl_setopt ( $cshnr , CURLOPT_POST ,  1 ) ;

$post  =  array (

"key1"  =>  "123" ,

"key2"  =>  "abc"

) ;

curl_setopt ( $cshnr , CURLOPT_POSTFIELDS ,  $post ) ;

$nr  =  curl_exec ( $cshnr ) ;

curl_close ( $cshnr ) ;

print_r ( $nr ) ;

?>

curl扩展方式提交post与get相似,首先curl_ini初始化,然后curl_setopt设置head信息,CURLOPT_POST设置为post提交,CURLOPT_POSTFIELDS设置提交的参数,然后执行curl,得到远程请求的服务端内容.

TAGS: php 表单
热门文章
最新文章

热门标签

数学计算路径命令行表单字符符号nginxcookiejavapython数组文字处理小数服务器管理apache图片处理