Issue
I want to send a request to a web service via an API as shown below. I have to pass a custom HTTP header (Hash), I'm using CURL, my code seems to work but I'm not getting the right response. I'm told it has to do with the hash value, though the value has been seen to be correct. Is there anything wrong with the way I'm passing it or with the code itself?
<?php
$ttime=time();
$hash="123"."$ttime"."dfryhmn";
$hash=hash("sha512","$hash");
$curl = curl_init();
curl_setopt($curl,CURLOPT_HTTPHEADER,array('Hash:$hash'));
curl_setopt ($curl, CURLOPT_URL, 'http://web-service-api.com/getresult.xml?clientid=456&time=$ttime');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$xml = curl_exec ($curl);
if ($xml === false) {
die('Error fetching data: ' . curl_error($curl));
}
curl_close ($xml);
echo htmlspecialchars("$xml", ENT_QUOTES);
?>
Solution
'Hash:$hash'
should be either "Hash: $hash"
(double quotes) or 'Hash: '.$hash
The same goes for your URL passed in CURLOPT_URL
Answered By - dev-null-dweller Answer Checked By - Willingham (WPSolving Volunteer)