Web login Authorization verification

Source: Internet
Author: User
When you access the data of a service software on the server through Http, the browser will pop up the following verification dialog box: because you want to use the PHPcurl method to obtain data, but do not know how to set the user name and password verification parameters: this does not work: {code ...} after using postman to simulate login, I found that...

When you access the data of a service software on the server over Http, the following verification dialog box is displayed in the browser:

I want to use the PHP curl method to obtain data, but I don't know how to set the user name and password verification parameters:
This discovery does not work:

curl_setopt($curl, CURLOPT_USERPWD, "name:pwd"); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 

After using postman to simulate login, I found that there is one in the request header:

Then add a request header to the PHP curl method:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json','Authorization:Basic TnhFY29AMjAxNjojQGlIeVg0eStHUFE='));

In this way, you can request data.
However, the problem is that the script needs to be put on the server for continuous operation. this write-dead verification will fail again after expiration, and there is no permission.
How can I set the php Curl username and password parameters to verify logon?

Reply content:

When you access the data of a service software on the server over Http, the following verification dialog box is displayed in the browser:

I want to use the PHP curl method to obtain data, but I don't know how to set the user name and password verification parameters:
This discovery does not work:

curl_setopt($curl, CURLOPT_USERPWD, "name:pwd"); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 

After using postman to simulate login, I found that there is one in the request header:

Then add a request header to the PHP curl method:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json','Authorization:Basic TnhFY29AMjAxNjojQGlIeVg0eStHUFE='));

In this way, you can request data.
However, the problem is that the script needs to be put on the server for continuous operation. this write-dead verification will fail again after expiration, and there is no permission.
How can I set the php Curl username and password parameters to verify logon?

This is php http verification. this is generally not the case. after all, HTTP Verification is too simple.

Http://php.net/manual/zh/feat...

$username='ABC';$password='XYZ';$URL='
  
   ';$ch = curl_init();curl_setopt($ch, CURLOPT_URL,$URL);curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 secondscurl_setopt($ch, CURLOPT_RETURNTRANSFER,1);curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code$result=curl_exec ($ch);curl_close ($ch);
  

  1. Use CURL to simulate logon to get the Authorization Token

  2. Use the Token obtained in step 1 for the next request.

The Token in step 1 can be properly cached, and the cache time depends on the server.
Digress: I personally think the server side is written in yii2.

Having done something similar before, I thought it was too troublesome for a long time and finally solved it with phantomjs. Php calls the node program.

Add this attribute

$user = 'hello';$pass = '$world';curl_setopt($ch, CURLOPT_USERPWD, "{$user}:{$pass}");  
Related Article

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.