网站首页 > 文章中心 > 其它

php数据的请求

作者:小编 更新时间:2023-09-08 18:15:12 浏览量:188人看过

php请求第三方数据方法

方法有很多,其中有file_get_contents把数据读到一个字符串中,还有一个是curl方式,两种方式有所不同,查一下就知道区别了

php中有哪些常用的远程请求发送方法

php数据的请求-图1

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

php

$url = '' ;

$html = file_get_contents ( $url );

echo $html ;

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

stream_get_meta_data( $fp );

while (! feof ( $fp )) {

}

echo "url body: $result" ;

php数据的请求-图2

fclose( $fp );

$data = array ( 'foo' = 'bar' );

$data = http_build_query($data);

$opts = array (

'http' = array (

'method' = 'POST' ,

'header' = "Content-type: application/x-www-form-urlencodedrn" . 'Content-Length: ' . strlen($data) . 'rn' , 'content' = $data ) ); $context = stream_context_create($opts); $html = file_get_contents( '' , false , $context); echo $html;

function get_url ( $url , $cookie =false)

{

$url = parse_url ( $url );

$query = $url [path]. '?' . $url [query];

echo 'Query:' . $query ;

if (! $fp ) {

return false;

} else {

$request = 'GET $query HTTP/1.1rn' ;

$request .= 'Host: $url[host]rn' ;

$request .= 'Connection: Closern' ;

if ( $cookie ) $request .= 'Cookie: $cookien' ;

$request .= 'rn' ;

fwrite( $fp , $request );

while (!@ feof ( $fp )) {

return $result ;

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

function GetUrlHTML( $url , $cookie =false)

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

if ( $rowdata )

$body = stristr ( $rowdata , 'rnrn' );

return $body ;

function HTTP_Post( $URL , $data , $cookie , $referrer = '' )

// parsing the given URL

$URL_Info = parse_url ( $URL );

// Building referrer

if ( $referrer == '' ) // if not given use this script as referrer

$referrer = '111' ;

// making string from $data

foreach ( $data as $key = $value )

$values []= '$key=' .urlencode( $value );

$data_string =implode( '' , $values );

if (!isset( $URL_Info [ 'port' ]))

// building POST-request:

$ch = curl_init();

curl_setopt ( $ch , CURLOPT_URL, 'http: //');

curl_setopt ( $ch , CURLOPT_RETURNTRANSFER, 1);

curl_setopt ( $ch , CURLOPT_CONNECTTIMEOUT, $timeout );

$file_contents = curl_exec( $ch );

curl_close( $ch );

echo $file_contents ;

以上就是土嘎嘎小编为大家整理的php数据的请求相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章