方法有很多,其中有file_get_contents把数据读到一个字符串中,还有一个是curl方式,两种方式有所不同,查一下就知道区别了
①.、用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" ;
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数据的请求相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!