Hello everone my name is reidho i have a problem with my code, i want to mirroring a api output from somesite using file_get_contents but the results is not like the original.

Original Result :
original.png
My Mirroring Result :
result.png
The Code :

$ping = "https://api.hackertarget.com/nping/?q=8.8.8.8";
$pingkeluar   = file($ping);
foreach ($pingkeluar as $pingnya) {
echo " $pingnya ";
}

Dani AI

Generated

Short version: the API is returning plain-text (monospace/column layout) but your page is rendering it as HTML, so newlines and repeated spaces disappear. correctly pointed out you need to fetch the body; the missing step is preserving whitespace and escaping any characters the browser would treat as HTML. The screenshots show a preformatted/text output, so treat the reply as raw text when you mirror it.

Common causes and fixes

  • HTML collapses whitespace and strips leading spaces. Wrap the response in a <pre> (or use CSS white-space: pre-wrap) and escape it with htmlspecialchars so the browser shows the exact characters.
  • Different line endings (CRLF vs LF) can change layout; normalize them to "\n".
  • Some command-line-style outputs include ANSI escape (color) codes — those are invisible in HTML and should be removed or converted to HTML spans if you need colors.
  • For reliability use a server-side HTTP client (cURL) and check HTTP status and errors rather than assuming the fetch always succeeds.

Example workflow (safe, robust fetch + preserve formatting)

<?php
$url = 'https://api.hackertarget.com/nping/?q=8.8.8.8';

$ch = curl_init();
curl_setopt_array($ch, [
  CURLOPT_URL             => $url,
  CURLOPT_RETURNTRANSFER  => true,
  CURLOPT_FOLLOWLOCATION  => true,
  CURLOPT_TIMEOUT         => 10,
  CURLOPT_USERAGENT       => 'mirror-script/1.0'
]);

$response = curl_exec($ch);
$info = curl_getinfo($ch);
$err  = curl_error($ch);
curl_close($ch);

if ($response === false || ($info['http_code'] ?? 0) >= 400) {
  header('Content-Type: text/plain; charset=utf-8');
  echo "Fetch error: " . ($err ?: 'HTTP ' . ($info['http_code'] ?? 'unknown'));
  exit;
}

$response = str_replace(["\r\n", "\r"], "\n", $response);
$response = preg_replace('/\e\[[0-9;]*[A-Za-z]/', '', $response);

echo '<pre style="white-space: pre-wrap; font-family: monospace;">'
   . htmlspecialchars($response, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8')
   . '</pre>';

Quick troubleshooting

  • If no data appears, confirm your server can make outbound requests (firewall, DNS, allow_url_fopen setting if using streams).
  • If the remote blocks your requests, try a sensible User-Agent and respect rate limits.
  • Never echo unescaped remote HTML into a page — that creates XSS risks.

This expands on ’s fetch idea and shows how to preserve the original formatting seen in the screenshots for .

So I think you have two options:

$file_as_array = file("https://api.hackertarget.com/nping/?q=8.8.8.8");
foreach ($file AS $array AS $line_of_file)
{
    echo "$line_of_file\n"; // \n is used to put a hard return line break at the end of each line
}

Or:

echo file_get_contents("https://api.hackertarget.com/nping/?q=8.8.8.8");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.