New for REST API

Basically, I was tasked with creating a REST request application in PHP using a third-party REST API. Doing POST, GET, etc. It seems simple, however, they have something called an authorization header, which uses the Digest token. How to pass this through get?

EG:

$url = "http:/domain/core.xml"; $response = file_get_contents($url); echo $response; 

Refund: Could not find digest header headers

In FireFoxes POSTER, I would just add the "Authorization" header with the value "Digest 0: codehere" and it works.

+6
source share
3 answers

See file_get_contents ()

 string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] ) 

The third argument to $context allows you to add the context created by stream_context_create () . See HTTP Context Settings . There you can find the header option, which allows you to set the headers that will be used by the request you sent, in your case Authorization -header

+9
source

You can use curl for Rest calls. For your help this is a reference link: auth token with curl

What is curl

+5
source
 <?php $url ="http://example.com/target.php"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_USERPWD,'username:password'); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); $result = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); ?> 

On the server side (target.php), we can access the username and password as follows.

 $USERNAME = $_SERVER['PHP_AUTH_USER']; $PASSWORD = $_SERVER['PHP_AUTH_PW']; 

In some cases, the $ _SERVER variables are NOT AVAILABLE to YOUR LOCAL SERVER. SO PLEASE NEXT CODE IN YOUR TENSION. HTTP AUTHORIZATION MODULE WILL WORK IN PHP. REASON - THIS WE CAN INSTALL THIS AS A SEPARATE MODULE

 <IfModule mod_rewrite.c> RewriteEngine on RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L] </IfModule> 
+3
source

Source: https://habr.com/ru/post/888534/


All Articles