php地址请求_php手册地址
php如何获取请求接口资源的请求者的IP地址
/**
*?@param?integer?$type
*?@return?mixed
*/
function?getclientip()?{
?static?$realip?=?NULL;
?
?if($realip?!==?NULL){
??return?$realip;
?}
?if(isset($_SERVER)){
??$arr?=?explode(',',?$_SERVER['HTTP_X_FORWARDED_FOR']);
??/*?取X-Forwarded-For中第一个非unknown的有效IP字符串?*/
??foreach?($arr?AS?$ip){
???$ip?=?trim($ip);
???if?($ip?!=?'unknown'){
????$realip?=?$ip;
????break;
???}
??}
??}elseif(isset($_SERVER['HTTP_CLIENT_IP'])){//HTTP_CLIENT_IP?是代理服务器发送的HTTP头.如果是"超级匿名代理",则返回none值.同样,REMOTE_ADDR也会被替换为这个代理服务器的IP.
??$realip?=?$_SERVER['HTTP_CLIENT_IP'];
??}else{
??if?(isset($_SERVER['REMOTE_ADDR'])){?//正在浏览当前页面用户的?IP?地址
???$realip?=?$_SERVER['REMOTE_ADDR'];
??}else{
???$realip?=?'0.0.0.0';
??}
?}else{
??//getenv环境变量的值
??$realip?=?getenv('HTTP_X_FORWARDED_FOR');
??$realip?=?getenv('HTTP_CLIENT_IP');
??$realip?=?getenv('REMOTE_ADDR');?//正在浏览当前页面用户的?IP?地址
?$realip?=?!empty($onlineip[0])?$onlineip[0]?:?'0.0.0.0';
?return?$realip;
}
如何用php向服务器发送post请求
用PHP向服务器发送HTTP的POST请求,代码如下:
php
/**?
*?发送post请求?
*?@param?string?$url?请求地址?
*?@param?array?$post_data?post键值对数据?
*?@return?string?
*/?
function?send_post($url,?$post_data)?{?
?$postdata?=?http_build_query($post_data);?
?$options?=?array(?
??'http'?=?array(?
???'method'?=?'POST',?
???'header'?=?'Content-type:application/x-www-form-urlencoded',?
???'content'?=?$postdata,?
??)?
?);?
?$context?=?stream_context_create($options);?
?$result?=?file_get_contents($url,?false,?$context);???
?return?$result;?
使用的时候直接调用上面定义的send_post方法:
$post_data?=?array(
'username'?=?'username',
'password'?=?'password'
);
send_post('网址',?$post_data);
thinkphp5地址路由请求响应错误
路由到read操作
路由到archive操作
项目配置文件中的路由定义如下:
//启用路由功能
'URL_ROUTER_ON'=true,
//路由定义
'URL_ROUTE_RULES'= array(
'blog/:year\d/:month\d'='Blog/archive', //规则路由
'blog/:id\d'='Blog/read', //规则路由
'blog/:cate'='Blog/category', //规则路由
'/(\d+)/' = 'Blog/view?id=:1',//正则路由
),
在模板文件中,我们使用了U函数动态生成路由地址:
路由1:blog/curd
php请求url并接收返回值
方法1:?用file_get_contents?以get方式获取内容
[php]?view?plaincopyprint?
php?
$url='';?
$html?=?file_get_contents($url);?
echo?$html;?
$fp?=?fopen($url,?'r');?
//返回请求流信息(数组:请求状态,阻塞,返回值是否为空,返回值http头等)?
stream_get_meta_data($fp);?
while(!feof($fp))?{?
}?
echo?"url?body:?$result";?
fclose($fp);?
使用$_POST['参数名']处理post方法提交的参数,$_GET['参数名']处理get方法参数.
eg:
$name = $_GET['name'];
$pwd = $_GET['pwd'];
do something;
如果url 为: index.html
$name = $_POST['name'];
$pwd = $_POST['pwd'];
如果只是处理如何要跳转到其他页面,可以用header("Location: 文件名");
如果是网页和php混合,在需要使用?php php语句;?处理就行;使用echo可以输出一些值到网页中.
以上就是特宜百科网小编为大家整理的php地址请求相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!