'admin', 'password' => 'Sophal@Crm@Sophal', ]); $contextLogin = stream_context_create([ 'http' => [ 'method' => 'POST', 'header' => "Content-Type: application/x-www-form-urlencoded\r\n", 'content' => $post, ] ]); file_get_contents($loginUrl, false, $contextLogin); // Extract session cookie $cookies = []; foreach ($http_response_header as $hdr) { if (stripos($hdr, 'Set-Cookie:') === 0) { $cookies[] = trim(substr($hdr, 11), ';'); } } $cookieHeader = 'Cookie: ' . implode('; ', $cookies); // 2) Get PDF $contextPDF = stream_context_create([ 'http' => [ 'method' => 'GET', 'header' => $cookieHeader ] ]); $pdfContent = file_get_contents($exportUrl, false, $contextPDF); // 3) Validate if (strpos($pdfContent, '%PDF') !== 0) { error_log("ExportPDF returned invalid PDF for record $recordId"); return; } // 4) Save $filePath = $_SERVER['DOCUMENT_ROOT']."/sophalcrm/storage/SalesOrder_$recordId.pdf"; file_put_contents($filePath, $pdfContent);