php怎么获取远程页面中的一个div的内容

php怎么获取远程页面中的一个div的内容,第1张

获取远程页面可以用file_get_contents()函数或者curl拓展,后者的效率会更好,但需要你修改一些配置。

查找html页面中的内容或者用phpQuery或这个:>

1、<php

2、$url='>

3、$html=file_get_contents($url);

4、echo$html;

5、>

二、用file_get_contents函数,以post方式获取url,需要输入内容为

1、<php

2、$url='>

3、$data=array('foo'=>'bar');

4、$data=>

5、$opts=array(

6、'>

7、 'method'=>'POST',

8、 'header'=>"Content-type:application/x->

9、          "Content-Length:"strlen($data)"\r\n",

10、 'content'=>$data

11、)

12、);

13、$ctx=stream_context_create($opts);

14、$html=@file_get_contents($url,'',$ctx);

15、>

三、用fopen打开url,以get方式获取内容,需要输入内容为

1、<php

2、$fp=fopen($url,'r');

3、$header=stream_get_meta_data($fp);//获取信息

4、while(!feof($fp)){

5、$result=fgets($fp,1024);

6、}

7、echo"urlheader:{$header}<br>":

8、echo"urlbody:$result";

9、fclose($fp);

10、>

四、用fopen打开url,以post方式获取内容,需要输入内容为

1、<php

2、$data=array('foo2'=>'bar2','foo3'=>'bar3');

3、$data=>

4、$opts=array(

5、'>

6、'method'=>'POST',

7、'header'=>"Content-type:application/x->

8、"Content-Length:"strlen($data)"\r\n",

9、'content'=>$data

10、)

11、);

12、$context=stream_context_create($opts);

13、$html=fopen('>

14、$w=fread($html,1024);

15、echo$w;

16、>

五、用fsockopen函数打开url,以get方式获取完整的数据,包括header和body,需要输入内容为

1、php

2、functionget_url($url,$cookie=false)

3、{

4、$url=parse_url($url);

5、$query=$url[path]""$url[query];

6、echo"Query:"$query;

7、$fp=fsockopen($url[host],$url[port]$url[port]:80,$errno,$errstr,30);

8、if(!$fp){

9、returnfalse;

10、}else{

11、$request="GET$query>

12、$request="Host:$url[host]\r\n";

13、$request="Connection:Close\r\n";

14、if($cookie)$request="Cookie:  $cookie\n";

15、$request="\r\n";

16、fwrite($fp,$request);

17、while(!@feof($fp)){

18、$result=@fgets($fp,1024);

19、}

20、fclose($fp);

21、return$result;

22、}

23、}

24、//获取url的html部分,去掉header

25、functionGetUrlHTML($url,$cookie=false)

26、{

27、$rowdata=get_url($url,$cookie);

28、if($rowdata)

29、{

30、$body=stristr($rowdata,"\r\n\r\n");

31、$body=substr($body,4,strlen($body));

32、return$body;

33、}

34、 returnfalse;

35、}

36、>

参考资料:

php-file_get_contents

这个问题比较笼统啊,

如果是想提交的内容,input放在表单里,

如果想用异步,使用jquery,获取input元素的值然后用ajax传递给php

给你举个例子

<form action="" method="POST">

<input name="aaa" value=“”>

<input type="submit">

</form>表单

点击提交后,会把aaa里的值传递给当前页面,使用post的方式

获取使用$_POST['aaa']就可以了;

使用jquery

<script>

var ipt = $('input[name=\'aaa\']')val();

$get('testphpinpt='+ipt, function(data){

alert(data);

});

</script>

使用get的方式传递给testphp,testphp中使用$_GET['inpt']获取

这是请求报头中的Accept-Language的关系了。

这样一来,你只是使用file_get_contents函数就实现不了这个功能了。

能够模拟浏览器发送各种报头,甚至是登录用cookie的扩展有不少,Curl就是其中之一。

你需要去网上去寻找相关的文章仔细了解一番,再做打算:

下面是PHP官网关于CURL扩展的手册:

Client URL Library>

以上就是关于php怎么获取远程页面中的一个div的内容全部的内容,包括:php怎么获取远程页面中的一个div的内容、PHP怎么获取表单提交的数据啊、php怎么获取文本框的内容等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9620075.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存