I’m trying to run a curl query to my website to retreive the body, it should be a html returned. There is a redirect before reaching the final url. When I run it from my command line, I get the expected result :
I’m attempting to execute a curl command on my website to retrieve the HTML content of the page. There’s a redirect that occurs before reaching the final URL. When I run the command in my terminal, the result is as expected.
curl.exe https://example.com/saw/ess?TENANTID=123-L
Response:
<!DOCTYPE HTML>
<html>
<head>
<title>Bad request</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta http-equiv="x-ua-compatible" content="IE=edge" />
<link rel="shortcut icon" href="/bo/favicon.ico"/>
<style>
@font-face {
font-family: 'Metric-Light';
src: url('/bo/fonts/Metric-Light.eot?#iefix') format('embedded-opentype'),
url('/bo/fonts/Metric-Light.woff') format('woff'),
url('/bo/fonts/Metric-Light.ttf') format('truetype')
}
body {
font-family: "Metric-Light", "Helvetica Neue", "Helvetica", "Arial", "sans-serif";
}
.content {
}
.logo {
display: block;
margin-top: 10%;
margin-left: auto;
margin-right: auto;
}
.title{
font-size: 70px;
text-align: center;
color: #6f6f6f;
text-transform: capitalize;
display: block;
margin-top: 3%;
}
.exception{
font-size: 30px;
text-align: center;
color: #6f6f6f;
display: block;
margin-top: 15px;
}
.detail{
margin-top: 20px;
font-size: 20px;
text-align: center;
color: #6f6f6f;
display: block;
}
</style>
</head>
<body>
<div class="content">
<img class="logo" src="/bo/fonts/Bad_Request_Content.png" alt="error"/><br/>
<span class="title">Bad request</span><br/>
<span class="exception">Bad request</span>
<p class="detail">Please try again or contact your local IT person.</p>
</div>
</body>
</html>
The result is as expected. Now, I’d like to retrieve the content using PHP.
I attempted the following code:
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://example.com/saw/ess?TENANTID=123");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Follow redirects
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Set headers to mimic the Chrome curl command
curl_setopt($curl, CURLOPT_HTTPHEADER, [
"user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
]);
$response = curl_exec($curl);
if ($response === false) {
echo "cURL Error: " . curl_error($curl);
curl_close($curl);
exit;
}
curl_close($curl);
var_dump($response);
Output:
<!doctype html>
<html dir="ltr" lang="en">
<head>
<meta charset="utf-8">
<title>Idm Service</title>
<base href="/idm-service/idm/v0/">
<meta name="viewport" http-equiv="X-UA-Compatible" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="styles.a06961a3935e65df.css"></head>
<body>
<app-root></app-root>
<script src="runtime.36808ec0bfe1e098.js" type="module"></script><script src="polyfills.abbcd89f1033283d.js" type="module"></script><script src="main.99cb60abd387e4e1.js" type="module"></script></body>
</html>
How can I retrieve the same output as in the command line?