phpstream_context_create混淆

发布于 2024-11-14 07:05:05 字数 1923 浏览 2 评论 0原文

我知道 php 中的此功能会创建并返回一个流上下文,其中包含选项预设中提供的任何选项。我也知道我可以用它来做我想做的事情,即传递我的用户名和密码凭据...但我仍然不太明白如何使用此功能以及它到底是做什么的,描述中的复杂单词是什么真的让我很困惑......我有示例中的代码,但我不知道如何使用此功能将凭据传递到 www.confluence.com (只有我可以访问它,因为它位于 apache 服务器上)。有人可以解释或举例说明如何使用它来传递凭据吗?

编辑:这是总体摘要,很短,所以不知道您是否称其为摘要......我被分配了一个应用程序来制作......大约有 10 种不同的方法来做到这一点。由于他们给我的限制,我只能使用我发现的一种方式...我非常沮丧,因为使用谷歌日历有一种更简单的方法可以做到这一点,但他们出于安全原因拒绝,所以我陷入困境到 confluence 日历......除此之外,为了让这变得更难,confluence 是由外部公司托管的,所以我什至不能使用 get_contents 直接访问 confluence 日历,因为它要求登录凭据......我不是一个专业人士,一个又一个的障碍不断出现,让我的生活变得更加困难,这只是胡说八道……在过去的三周里,我花了好几个小时寻找解决方案,结果却被拒绝了……我终于得到了这个 get_contents东西可以工作,但现在登录凭据是一个痛苦的屁股,我从来没有处理过这个,所以我尽力而为,但我绝对不知道stream_context_create做了什么,或者如何使用它来传递我的凭据...Confluence网站我正在尝试访问我可以使用我的登录名手动登录,但代码不能...confluence位于apache服务器上,因此其他人无法访问它...并且我无法共享我的登录信息,因为这是公司的安全问题。 ..如果我没有任何意义,我很抱歉,但我非常沮丧,找不到解决方案,而且我的思想因编码和阅读而半死不活 相信我,我已经做了很多搜索和谷歌搜索......但我终于达到了我陷入空白的地步......

<?php
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n"
  )
);

$context = stream_context_create($opts);

/* Sends an http request to www.example.com
   with additional headers shown above */
$fp = fopen('http://www.example.com', 'r', false, $context);
fpassthru($fp);
fclose($fp);
?>

我在这里发现了一些与我正在做的类似的附加代码,但不太确定如何处理这个或如何做修改它..

$data = array('account'=>'javier', 
              'password'=>'12345', 
              'submit'=>'SUBMIT'); 

$content = file_get_contents('http://localhost/misc/login.php', false, stream_context_create( array('http' => array('method' => 'POST', 'content' => http_build_query($data) ) ) ) ); 

$sFind = 'Logged in'; 
$search = strpos($content, $sFind); 

echo $content; 

if($search === false){ 
echo 'Invalid Account'; 
} 
else { 
echo 'Valid Account'; 
}  

我的另一个想法是使用JavaScript来进行登录...我不知道哪个更好,但我不允许下载任何库或类似的东西,所以建议很好。

I know that this feature in php creates and returns a stream context with any options supplied in options preset. I also know that I can use it to do what I want which is to pass my username and password credentials...But I still don't quite get how to use this feature and what it exactly does the complicated words in the description are really confusing me....I have the code from the example but I don't know how I can use this feature to pass credentials to www.confluence.com (only I can access it since its on a apache server). Can someone please explain or give example of how I can use this to pass the credentials?

EDIT: Here is the overal summary, pretty short so dunno if you call it a summary....I am assigned an app to make.....there is about 10 different ways to do this. due to the limitations they have given me, I can only work with 1 way that I found...I am very frustrated because there is a much easier way to do this using google calendar but they refuse due to security reasons so I am stuck to confluence calendar.....In addition to that, to make this harder, confluence is hosted by external company and so I cannot even use get_contents to directly access the confluence calendar because it asks for login credentials....I am not a pro at this and one after another obstacles keep popping up to make my life harder and this is just bs...Ive spent hours and hours for the past 3 weeks finding solutions only to have it rejected...Ive finally got this get_contents thingy working but now login credentials is a pain in the butt and I have never dealt with this so I am trying my best but I have ABSOLUTELY NO IDEA what stream_context_create does OR how to even use it to pass my credentials...Confluence the site I am trying to access I can manually login using my logins but the code cannot....confluence is on an apache server so other people cannot access it....and I cannot share my login info as that is company's security issue...I am sorry if i made no sense but I am very frustrated and can find no solution and my mind is half dead from coding and reading
Believe me I have done much searching and googling....But Ive finally reached the point where I am blank stuck...

<?php
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n"
  )
);

$context = stream_context_create($opts);

/* Sends an http request to www.example.com
   with additional headers shown above */
$fp = fopen('http://www.example.com', 'r', false, $context);
fpassthru($fp);
fclose($fp);
?>

I found some additional code here similar to what I am doing but not quite sure what to do with this or how to modify it..

$data = array('account'=>'javier', 
              'password'=>'12345', 
              'submit'=>'SUBMIT'); 

$content = file_get_contents('http://localhost/misc/login.php', false, stream_context_create( array('http' => array('method' => 'POST', 'content' => http_build_query($data) ) ) ) ); 

$sFind = 'Logged in'; 
$search = strpos($content, $sFind); 

echo $content; 

if($search === false){ 
echo 'Invalid Account'; 
} 
else { 
echo 'Valid Account'; 
}  

Another Idea I have is to use JavaScript to do the login...I dont know which is better but I am not allowed to download any libraries or such so suggestions are nice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水波映月 2024-11-21 07:05:05

您正在使用“GET”方法,这样传递登录信息的情况并不常见。
您确定您请求的页面没有使用 POST 吗?

就个人而言,在检索网页时,尤其是在发送 POST 或 GET 变量时,我更喜欢使用 cURL

You are using the 'GET' method, it is uncommon for login information to be passed like this.
Are you sure the page you are requesting doesn't use POST?

Also personally when retrieving webpages, especially when sending POST or GET variables I prefer to use cURL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文