本篇文章主要介绍PHP实现curl或file_get_contents 获取需要授权页面的方法,感兴趣的朋友参考下,希望对大家有所帮助。
例如要获取的页面:http://localhost/server.php
1 2 3 4 5 | <?php
$content = isset( $_POST ['content'])? $_POST ['content'] : '';
header('content-type:application/json');
echo json_encode( array ('content'=> $content ));
?>
|
登录后复制
使用curl获取server.php页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php
$url = 'http:
$param = array ('content'=>'fdipzone blog');
$ch = curl_init();
curl_setopt( $ch , CURLOPT_URL, $url );
curl_setopt( $ch , CURLOPT_POST, true);
curl_setopt( $ch , CURLOPT_POSTFIELDS, http_build_query( $param ));
curl_setopt( $ch , CURLOPT_RETURNTRANSFER, true);
$ret = curl_exec( $ch );
$retinfo = curl_getinfo( $ch );
curl_close( $ch );
if ( $retinfo ['http_code']==200){
$data = json_decode( $ret , true);
print_r( $data );
} else {
echo 'POST Fail';
}
?>
|
登录后复制
如果服务没有安装php curl扩展,使用file_get_contents也可以实现发起请求,获取页面返回数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php
$url = 'http:
$param = array ('content'=>'fdipzone blog');
$opt = array (
'http' => array (
'method' => 'POST',
'header' => 'content-type:application/x-www-form-urlencoded',
'content' => http_build_query( $param )
)
);
$context = stream_context_create( $opt );
$ret = file_get_contents ( $url , false, $context );
if ( $ret ){
$data = json_decode( $ret , true);
print_r( $data );
} else {
echo 'POST Fail';
}
?>
|
登录后复制
使用curl 和 file_get_contents 返回的结果都是一样的。
1 2 3 4 | Array
(
[content] => fdipzone blog
)
|
登录后复制
对于需要授权的页面,例如使用了htpasswd+.htaccess设置目录访问权限的页面,直接用上面的方法会返回401 Unauthorized错误。
这次的例子先不使用htpasswd+.htaccess来控制访问权限,而使用 $_SERVER['PHP_AUTH_USER'] 和 $_SERVER['PHP_AUTH_PW']这两个服务器参数。
http://localhost/server.php 修改为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php
if (!isset( $_SERVER ['PHP_AUTH_USER']))
{
header('WWW-Authenticate: Basic realm= "localhost" ');
header( "HTTP/1.0 401 Unauthorized" );
exit ;
} else {
if (( $_SERVER ['PHP_AUTH_USER']!= "fdipzone" || $_SERVER ['PHP_AUTH_PW']!= "654321" )) {
header('WWW-Authenticate: Basic realm= "localhost" ');
header( "HTTP/1.0 401 Unauthorized" );
exit ;
}
}
$content = isset( $_POST ['content'])? $_POST ['content'] : '';
header('content-type:application/json');
echo json_encode( array ('content'=> $content ));
?>
|
登录后复制
设定帐号:fdipzone 密码:654321
curl中,有一个参数是 CURLOPT_USERPWD,我们可以利用这个参数把帐号密码在请求时发送过去。
curl_setopt($ch, CURLOPT_USERPWD, '帐号:密码');
curl请求的程序修改为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php
$url = 'http:
$param = array ('content'=>'fdipzone blog');
$ch = curl_init();
curl_setopt( $ch , CURLOPT_URL, $url );
curl_setopt( $ch , CURLOPT_POST, true);
curl_setopt( $ch , CURLOPT_POSTFIELDS, http_build_query( $param ));
curl_setopt( $ch , CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch , CURLOPT_USERPWD, 'fdipzone:654321');
$ret = curl_exec( $ch );
$retinfo = curl_getinfo( $ch );
curl_close( $ch );
if ( $retinfo ['http_code']==200){
$data = json_decode( $ret , true);
print_r( $data );
} else {
echo 'POST Fail';
}
?>
|
登录后复制
而file_get_contents 如果要发送帐号和密码,需要手动拼接header
file_get_contents 请求的程序修改为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?php
$url = 'http:
$param = array ('content'=>'fdipzone blog');
$auth = sprintf('Authorization: Basic %s', base64_encode ('fdipzone:654321'));
$opt = array (
'http' => array (
'method' => 'POST',
'header' => "content-type:application/x-www-form-urlencoded\r\n" . $auth . "\r\n" ,
'content' => http_build_query( $param )
)
);
$context = stream_context_create( $opt );
$ret = file_get_contents ( $url , false, $context );
if ( $ret ){
$data = json_decode( $ret , true);
print_r( $data );
} else {
echo 'POST Fail';
}
?>
|
登录后复制
相关推荐:
PHP中file_put_contents函数详解
PHP使用file_get_contents发送http请求步骤详解
file_get_contents函数介绍与使用方法详解
以上是PHP实现curl或file_get_contents 获取需要授权页面的方法的详细内容。更多信息请关注PHP中文网其他相关文章!