title: PHP to retrieve the content of the Password Required pages once i have password?
body: <p>I need to retrieve the html contents (source) of the pages, for example: www.google.com page. Then i can use: <code>file_get_contents</code> or <code>curl_init</code> with PHP.</p>
  
  <p>Exactly as someones question before here:<br>
  <a href="https://stackoverflow.com/questions/819182/how-do-i-get-the-html-code-of-a-web-page-in-php">How do I get the HTML code of a web page in PHP?</a></p>
  
  <p>But more then that for me, some of the pages are <strong>Access Required</strong>.<br>
  <strong>But i have granted access and know the password.</strong><br></p>
  
  <p>(Lets say it ask password with a form and the password is "abcd".)</p>
  
  <p>So how do i read that pages programmatically with PHP?</p>
  
  <p>Updated (the answer, for me):<br>
  I found the solution with <code>curl-setopt</code> <a href="https://stackoverflow.com/questions/11719630/php-to-retrieve-the-content-of-the-password-required-pages-once-i-have-password#11719798">suggested by Bekzat Abdiraimov below</a>. Then now i posted detail the codes here that i found somewhere and modified:</p>
  
  <pre><code>&lt;?php
  function curl_grab_page($url, $ref_url, $data, $login, $proxy, $proxystatus){
      if($login == 'true') {
          $fp = fopen("cookie.txt", "w");
          fclose($fp);
      }
  
      $ch = curl_init();
  
      curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
      curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
      curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
      curl_setopt($ch, CURLOPT_TIMEOUT, 40);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  
      if ($proxystatus == 'true') {
          curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
          curl_setopt($ch, CURLOPT_PROXY, $proxy);
      }
  
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_REFERER, $ref_url);
  
      curl_setopt($ch, CURLOPT_HEADER, TRUE);
      curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
      curl_setopt($ch, CURLOPT_POST, TRUE);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  
      curl_exec($ch);
  
      curl_setopt($ch,CURLOPT_URL,$ref_url);
      curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
  
      ob_start();
      $data = curl_exec($ch);
      ob_end_clean();
  
      curl_close($ch);
      return $data;
  }
  
  /*
   * $auth_processing_url .. is the posted 'action' url in login form like &lt;form method=post action='http://www.abc.com/login.asp'&gt; So it should be like: "http://www.abc.com/login.asp"
   * $url_to_go_after_login .. is the url you want to go (to be redireced) after login
   * $login_post_values .. are the form input names what Login Form is asking. E.g on form: &lt;input name="username" /&gt;&lt;input name="password" /&gt;. So it should be: "username=4lvin&amp;password=mypasswd"
   */
  echo curl_grab_page($auth_processing_url, $url_to_go_after_login, $login_post_values, "true",  "null", "false");
  ?&gt;
  </code></pre>