0

I am trying to request an API and receive a blank response. The page source is giving me the same output as what I get in postman. But not sure if it is a string or XML format in the browser. Below is the code. Please help me what is the problem with this.

<?php

$username = 'XXXX';
$password = 'XXXX';
$url = 'https://10.5.6.1/getperformance';

$credentials = base64_encode("$username:$password");

$headers = [];
$headers[] = "Authorization: Basic {$credentials}";
$headers[] = 'Content-Type: application/xml';
$headers[] = 'Accept: */*';



$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);



$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}

curl_close($ch);

echo $result;
  • Get it working first from bash/command line, then try to make it into a PHP script. Also adding verbosity could be useful to see if the headers are the way you want and could compare it your other tools. Have a look if something here helps: https://stackoverflow.com/questions/3164405/show-curl-post-request-headers-is-there-a-way-to-do-this – Anton Krug Apr 13 '21 at 00:36
  • 1
    Everything you return from PHP is a string. When you look at 'view source', that _is_ what your server returned. A browser will not show anything that looks like a xml/html tag if your content-type is set to `text/html`, which is the default. – Evert Apr 13 '21 at 00:47
  • Try adding `header("Content-type: text/xml");` before `echo $result;` – Phil Apr 13 '21 at 00:53

0 Answers0