Hi, I don't use much curl and I need to do the following:
I have a template in PW, with the following code:
userid, reportID are received from a form in this same page.
$url = 'https://path.to.API.endpoint';
$ch = curl_init( $url );
$data_string = '{
"userid": "' . $userid . '",
"reportID": "' . $reportID . '"
}';
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_string );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Authorization: AUTHORIZATION KEY',
'Content-Type:application/json'
));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POST, true );
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result, true);
if($obj[0]['status'] === "validated") {
$pdf = $obj[0]['reportURL'];
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="report.pdf"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($pdf));
ob_clean();
flush();
readfile($pdf);
exit;
}
The problem I have is when I run the code above I get this error:
{"statusCode":400,"error":"Bad Request","message":"Invalid cookie value"}
The API developer tells me that the error occurs because when making the query using POST I am injecting my server's cookies into his server and that is why it gives that error and the call does not go through.
He told me that the solution is to take the response from the API and use ajax to deliver the PDF report, but I don't understand how to do all this in Processwire.
Any help is very welcome.