189 8069 5689

php抓取异步数据 php实现异步

PHP如何异步处理json返回数据

ajax会吗 不要给action加提交链接 给form设一个点击事件,用js获取input的值 用axaj提交并返回

公司主营业务:网站建设、成都做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联建站是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联建站推出唐河免费做网站回馈大家。

php怎么抓取其它网站数据

可以用以下4个方法来抓取网站 的数据:

1. 用 file_get_contents 以 get 方式获取内容:

?

$url = '';

$html = file_get_contents($url);

echo $html;

2. 用fopen打开url,以get方式获取内容

?

$url = '';

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

stream_get_meta_data($fp);

$result = '';

while(!feof($fp))

{

$result .= fgets($fp, 1024);

}

echo "url body: $result";

fclose($fp);

3. 用file_get_contents函数,以post方式获取url

?

$data = array(

'foo'='bar',

'baz'='boom',

'site'='',

'name'='nowa magic');

$data = http_build_query($data);

//$postdata = http_build_query($data);

$options = array(

'http' = array(

'method' = 'POST',

'header' = 'Content-type:application/x-www-form-urlencoded',

'content' = $data

//'timeout' = 60 * 60 // 超时时间(单位:s)

)

);

$url = "";

$context = stream_context_create($options);

$result = file_get_contents($url, false, $context);

echo $result;

4、使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展

$url = '';

$ch = curl_init();

$timeout = 5;

curl_setopt ($ch, CURLOPT_URL, $url);

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$file_contents = curl_exec($ch);

curl_close($ch);

echo $file_contents;

php如何实现脚本异步执行的方法具体分析

php语言得用fsockopen()函数,实现脚本异步运行,代码如下

异步请求函数(用debug参数若为true则为用为调试,开启调试可以看到异步的执行情况,但是失去异步的效果)

main.php

?php

/**

* 异步请求

* @copyright  Copyright (c) Hangzhou Technology Co.,Ltd. ()

* @author     $Author: juny $

* @version    $Id: main.php 332 2018-09-23 09:15:08Z juny $

*/

function request_by_fsockopen($url,$post_data=array(),$debug=false){

$url_array = parse_url($url);

$hostname = $url_array['host'];

$port = isset($url_array['port'])? $url_array['port'] : 80;

@$requestPath = $url_array['path'] ."?". $url_array['query'];

$fp = fsockopen($hostname, $port, $errno, $errstr, 10);

if (!$fp) {

echo "$errstr ($errno)";

return false;

}

$method = "GET";

if(!empty($post_data)){

$method = "POST";

}

$header = "$method $requestPath HTTP/1.1\r\n";

$header.="Host: $hostname\r\n";

if(!empty($post_data)){

$_post = strval(NULL);

foreach($post_data as $k = $v){

$_post[]= $k."=".urlencode($v);//必须做url转码以防模拟post提交的数据中有符而导致post参数键值对紊乱

}

$_post = implode('', $_post);

$header .= "Content-Type: application/x-www-form-urlencoded\r\n";//POST数据

$header .= "Content-Length: ". strlen($_post) ."\r\n";//POST数据的长度

$header.="Connection: Close\r\n\r\n";//长连接关闭

$header .= $_post; //传递POST数据

}else{

$header.="Connection: Close\r\n\r\n";//长连接关闭

}

fwrite($fp, $header);

//-----------------调试代码区间-----------------

//注如果开启下面的注释,异步将不生效可是方便调试

if($debug){

$html = '';

while (!feof($fp)) {

$html.=fgets($fp);

}

echo $html;

}

//-----------------调试代码区间-----------------

fclose($fp);

}

$data=array('name'='guoyu','pwd'='123456');

$url='';

request_by_fsockopen($url,$data,true);//

other.php

?php

header("content-type:text/html;charset=utf-8");

//error_reporting(0);

//ini_set('html_errors',false);

//ini_set('display_errors',false);

$name = isset($_POST['name'])?$_POST['name']:'';

$pwd = isset($_POST['pwd'])?$_POST['pwd']:'';

echo $name.$pwd;

echo 'success ok';

die;

?

使用实例:

[运行的main.php主脚本文件]

$data=array('name'='guoyu','pwd'='123456');

$url='';

request_by_fsockopen($url,$data,true);//把应用B的用户表异步-同步数据

[导步执行文件other.php]

在other.php中便可以用$_POST接收main.php提交过来的参数,从而进行下一步操作

以上就是php如何实现脚本异步执行的方法具体分析的详细内容.


标题名称:php抓取异步数据 php实现异步
文章路径:http://cdxtjz.cn/article/hehioo.html

其他资讯