http网址捆綁代理php_PHP 访问 HTTP(s) 网址

用户评论:

gaver at telenet dot be (2012-06-02 11:38:33)

If you want to communicate between your server and a https server

$variable=$_GET['variable'];$host="https://user:password@secure site";$path="securescript.php";$url=$host.$path;$formdata= array ("variable1"=>"10000","variable2"=>"02","variable3"=>"03");// get form data in a stringforeach($formdataAS$key=>$val){$poststring.=urlencode($key) ."=".urlencode($val) ."&";

}// strip off trailing ampersand$poststring=substr($poststring,0, -1);// create a complete nice string$urlcom=$host.$path."?".$poststring;// echo $urlcom; // if you want to debug your string

//and you can read out the data in one command... from a secure https server handling login and everything you need$postdataget=file_get_contents($urlcom);// and yes you can debug the responseheader to view if you have authorization

//  var_dump($http_response_header);

//and if you want to read the data, this is the response from the server

//  var_dump($postdataget);?>

Nick Lewis (2008-06-26 04:17:14)

A note on how to deal with Cookies

To receive a cookie:

$httphandle = fopen($url,"r");

$meta = stream_get_meta_data($httphandle);

for ($j = 0; isset($meta['wrapper_data'][$j]); $j++) {

$httpline = $meta['wrapper_data'][$j];

@list($header,$parameters) = explode(";",$httpline,2);

@list($attr,$value) = explode(":",$header,2);

if (strtolower(trim($attr)) == "set-cookie") {

$cookie = trim($value);

break;

}

}

fclose($httphandle);

echo $cookie;

To send a cookie:

$user_agent = ini_get("user_agent");

ini_set("user_agent",$user_agent . "\r\nCookie: " . $cookie);

$httphandle = fopen($url,"r");

fclose($httphandle);

ini_set("user_agent",$user_agent);

spazdaq (2007-10-24 03:27:46)

just an FYI about digest authentication.

While one of the above http examples has the username and password info supplied with the url, this must only be for basic authentication. it does not appear to work for digest authentication. you have to handle the digest followup request on your own.

NEA at AraTaraBul dot com (2007-07-29 16:06:50)

HTTP post function;

foreach($datastreamas$key=>$val) {

if (!empty($reqbody))$reqbody.="&";$reqbody.=$key."=".urlencode($val);

}$contentlength=strlen($reqbody);$reqheader="POST$uriHTTP/1.1\r\n"."Host:$host\n"."User-Agent: PostIt\r\n"."Content-Type: application/x-www-form-urlencoded\r\n"."Content-Length:$contentlength\r\n\r\n"."$reqbody\r\n";$socket=fsockopen($host,80,$errno,$errstr);

if (!$socket) {$result["errno"] =$errno;$result["errstr"] =$errstr;

return$result;

}fputs($socket,$reqheader);

while (!feof($socket)) {$result[] =fgets($socket,4096);

}fclose($socket);

return$result;

}?>

Sinured (2007-06-28 03:24:29)

If you want to send more than one custom header, just make header an array:

$default_opts= array('http'=> array('user_agent'=>'Foobar','header'=> array('X-Foo: Bar','X-Bar: Baz')

)

);stream_context_get_default($default_opts);readfile('http://www.xhaus.com/headers');?>

dwalton at acm dot org (2006-11-17 00:18:39)

As it says on this page:

"The stream allows access to the body of the resource; the headers are stored in the $http_response_header variable. Since PHP 4.3.0, the headers are available using stream_get_meta_data()."

This one sentence is the only documentation I have found on the mysterious $http_response_header variable, and I'm afraid it's misleading. It implies that from 4.3.0 onward, stream_get_meta_data() ought to be used in favor of $http_response_header.

Don't be fooled! stream_get_meta_data() requires a stream reference, which makes it ONLY useful with fopen() and related functions. However, $http_response_header can be used to get the headers from the much simpler file_get_contents() and related functions, which makes it still very useful in 5.x.

Also note that even when file_get_contents() and friends fail due to a 4xx or 5xx error and return false, the headers are still available in $http_response_header.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值