473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to send Username/Password and a Cookie with stream_context_create

2
I am trying to get the HTML code from a remote site, which creates different HTML output based on the cookie being sent.

So I am trying to send username/password and a cookie with stream_context_create() function.

It works without the $cookie in the header, but the I get the wrong HTML.

With the $cookie I get:
Warning: file_get_contents(http://www.url.com) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized in simple_html_dom.php on line 78

I think I just do not know the syntax. Please help.

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function file_get_html($url, $use_include_path = false, $context=null, $offset = -1, $maxLen=-1, $lowercase = true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT)
  3. {
  4.     $usernamepw = "username:password";
  5.     $cookie        = "skipPostLogin=1";
  6.     $context = stream_context_create(array(
  7.         'http' => array(
  8.             'header'  => "Authorization: Basic " . base64_encode($usernamepw) . $cookie, 
  9.             'timeout' => 60
  10.         )
  11.     ));
  12.     $dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $defaultBRText);
  13.     $contents = file_get_contents($url, $use_include_path, $context, $offset);
  14.     if (empty($contents))
  15.     {
  16.         return false;
  17.     }
  18.     $dom->load($contents, $lowercase, $stripRN);
  19.     return $dom;
  20. }
  21. ?>
  22.  
Apr 12 '15 #1
1 2654
sukrub
2
I have resolved the issue by changing the code as follows,

Expand|Select|Wrap|Line Numbers
  1. function file_get_html($url, $use_include_path = false, $context=null, $offset = -1, $maxLen=-1, $lowercase = true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT)
  2. {
  3.     $usernamepw = "username:password";
  4.     $cookie     = "skipPostLogin=1";
  5.  
  6.     $headers = array(
  7.         'Authorization: Basic ' . base64_encode($usernamepw),
  8.         'Cookie: ' . $cookie
  9.     );
  10.  
  11.     $context = stream_context_create(array(
  12.         'http' => array(
  13.             'header'  => $headers,
  14.             'timeout' => 60
  15.         )
  16.     ));
  17.     $dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $defaultBRText);
  18.     $contents = file_get_contents($url, $use_include_path, $context, $offset);
  19.     if (empty($contents))
  20.     {
  21.         return false;
  22.     }
  23.     $dom->load($contents, $lowercase, $stripRN);
  24.     return $dom;
  25. }
  26.  
Apr 12 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: deko | last post by:
I want to use htaccess for authentication on my php site since I only have a few users who need access to secure areas. So, I created a new directory off public_html (secretDocs) and in that...
4
by: Lobang Trader | last post by:
Hi all, I am trying to create a username and a password class. I would like to know what are the RECOMMENDED minimum and maximum length for both fields? These fields will be something like...
2
by: john brown | last post by:
There is a web page that I access frequently and would like to automate the authentication of the username and password. I would like to user a perl script but I'm not really sure about the steps....
5
by: Tony | last post by:
Please help, I've scoured the web but cannot find an answer to this! I want to use WebRequest to get the contents of a web page. The trouble is, the page require you to go to a login page (using...
0
by: Joey Edelstein | last post by:
Hi, We are trying to add a Client Certificate support for our web app that emulates a hardware device web app. The hardware uses a 2 factors authentication, which requires a Web Service client...
8
by: Noel Volin | last post by:
Anyone who can help here is much appreciated. I am trying to programmatically log onto a website. I am using the code provided in VS for the AuthenticationManager Class example (...
1
by: thoducng | last post by:
I am writing some code to access share folder on a remote network. DirectoryInfo dicInfo = new DirectoryInfo("remoteNetwork\shareFolder"); if (dicInfo.Exists) { //application code...
2
by: minhtran | last post by:
Hi All Anyone knows how to code(ASP.NET, C#,VB.NET) as to send username and password to Proxy Server required to reach web page (That means you code as programming automatically get web page without...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.