Home > Back-end >  How to Retrieve cURL Data within the URL Endpoint
How to Retrieve cURL Data within the URL Endpoint

Time:01-22

I am trying to create a cURL api, let's say the below is my curl request code, where http://localhost/api/endpoint.php is my endpoint. What would actually be going on, code-wise in the endpoint.php file? How do I actually get the data that is sent via the cURL request, like the CURLOPT_USERPWD data, in the endpoint.php page? Thanks.

$ch = curl_init();
$url = 'http://localhost/api/endpoint.php';

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERPWD, 'alex:password123');

$headers = array(
        'Accept: application/json',
        'Content-Type: application/x-www-form-urlencoded',
        'Accept-Language: en_US'        
);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

$vars['grant_type'] = 'client_credentials';

$req = http_build_query($vars);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);

$response = curl_exec($ch);

CodePudding user response:

You can access to these data via the $_SERVER superglobal.

Endpoint :

<?php

$user     = $_SERVER['PHP_AUTH_USER']; // alex
$password = $_SERVER['PHP_AUTH_PW'];   // password123

CodePudding user response:

in your file endpoint.php you can print your posted value using

print_r($_POST);

after your curl print response like

print_r($response);
  •  Tags:  
  • Related