PHP curl or file_get_contents to obtain the page authorization method, curlgetcontents

Source: Internet
Author: User

PHP curl or file_get_contents to obtain the page authorization method, curlgetcontents

Today, for work, you need to use curl/file_get_contents to obtain the page content for Authorization. After solving this problem, I wrote this article to share it with you.

PHP curl extension, which can initiate POST/GET requests on the server to access the page and obtain the returned data of the page.

For example, the page to be retrieved:Http: // localhost/server. php

<?php $content = isset($_POST['content'])? $_POST['content'] : ''; header('content-type:application/json'); echo json_encode(array('content'=>$content)); ?> 

Use curl to obtain the server. php page

<?php $url = 'http://localhost/server.php'; $param = array('content'=>'fdipzone blog'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $ret = curl_exec($ch); $retinfo = curl_getinfo($ch); curl_close($ch); if($retinfo['http_code']==200){  $data = json_decode($ret, true);  print_r($data); }else{  echo 'POST Fail'; } ?> 

If the service does not install the php curl extension, useFile_get_contentsYou can also initiate a request to obtain the data returned from the page.

<?php $url = 'http://localhost/server.php'; $param = array('content'=>'fdipzone blog'); $opt = array(  'http' => array(   'method' => 'POST',   'header' => 'content-type:application/x-www-form-urlencoded',   'content' => http_build_query($param)  ) ); $context = stream_context_create($opt); $ret = file_get_contents($url, false, $context); if($ret){  $data = json_decode($ret, true);  print_r($data); }else{  echo 'POST Fail'; } ?> 

The results returned by using curl and file_get_contents are the same.

Array (  [content] => fdipzone blog ) 

For pages to be authorized, for exampleHtpasswd +. htaccessThe page that sets the directory access permission.401 UnauthorizedError.

In this example, do not use htpasswd +. htaccess to control the access permission.$ _ SERVER ['php _ AUTH_USER ']And$ _ SERVER ['php _ AUTH_PW ']These two server parameters.

Http: // localhost/server. phpTo:

<?php if(!isset($_SERVER['PHP_AUTH_USER'])) {  header('WWW-Authenticate: Basic realm="localhost"');  header("HTTP/1.0 401 Unauthorized");  exit; }else{  if (($_SERVER['PHP_AUTH_USER']!= "fdipzone" || $_SERVER['PHP_AUTH_PW']!="654321")) {   header('WWW-Authenticate: Basic realm="localhost"');   header("HTTP/1.0 401 Unauthorized");   exit;  } } $content = isset($_POST['content'])? $_POST['content'] : ''; header('content-type:application/json'); echo json_encode(array('content'=>$content)); ?> 

Set Account: fdipzone password: 654321

In curl, there is a parameter:CURLOPT_USERPWDYou can use this parameter to send the account and password at the time of the request.

Curl_setopt ($ ch, CURLOPT_USERPWD, 'account: password ');

Modify the curl request program:

<? Php $ url = 'HTTP: // localhost/server. php '; $ param = array ('content' => 'fdipzone blog'); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url ); curl_setopt ($ ch, CURLOPT_POST, true); curl_setopt ($ ch, CURLOPT_POSTFIELDS, http_build_query ($ param); curl_setopt ($ ch, success, true); curl_setopt ($ ch, CURLOPT_USERPWD, 'fdipzone: 100'); // Add this sentence $ ret = curl_exec ($ ch); $ retinfo = curl_getinfo ( $ Ch); curl_close ($ ch); if ($ retinfo ['HTTP _ Code'] = 200) {$ data = json_decode ($ ret, true ); print_r ($ data);} else {echo 'Post Fail ';}?>

For file_get_contents, if you want to send the account and password, you need to manually splice the header

The file_get_contents request program is modified:

<? Php $ url = 'HTTP: // localhost/server. php '; $ param = array ('content' => 'fdipzone blog'); $ auth = sprintf ('authorization: Basic % s', base64_encode ('fdipzone: 654321 '); // Add this sentence $ opt = array ('http' => array ('method' => 'post ', 'header' => "content-type: application/x-www-form-urlencoded \ r \ n ". $ auth. "\ r \ n", // Add $ auth to header 'content' => http_build_query ($ param); $ context = stream_context_create ($ opt ); $ Ret = file_get_contents ($ url, false, $ context); if ($ ret) {$ data = json_decode ($ ret, true); print_r ($ data );} else {echo 'Post Fail ';}?>

Source code: Click to view

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.