[英]Setting CURL headers using CURLOPT_HTTPHEADER creates CORS problem
我正在嘗試使用 PHP 執行定義 HTTP 標頭的 cURL POST 請求,但我遇到了 CORS 問題。
所以我有一個使用 AJAX 從 webapp 調用的 .php 文件。 在 this.php 文件中,我正在使用 cURL 向外部 API 發出 HTTP POST 請求。 一切正常,直到我不得不為身份驗證目的設置不同的 HTTP 標頭。 當我嘗試使用以下命令在 cURL 請求中定義 HTTP 標頭時:
//Set Headers
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization': 'someAuthorization',
'x-api-key': 'somekey',
'Content-Type': 'application/x-www-form-urlencoded'
));
我開始在客戶端 (webapp) 和我自己的端點之間遇到 CORS 問題,這以前沒有發生過。 執行 cURL 請求后,我嘗試再次定義標頭,但沒有成功:
//execute post
$result = curl_exec($ch);
if($result === false){
echo 'Curl error: ' . curl_error($ch);
}else{
//$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//CORS
if (isset($_SERVER['HTTP_ORIGIN'])) {
//header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header("Access-Control-Allow-Origin: http://localhost:4200");
header('Access-Control-Allow-Credentials: true');
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
}
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers:
{$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
echo $result;
};
任何想法為什么會這樣? 在我看來,通過執行 cURL 請求,我似乎用 cURL 覆蓋了標頭,因此永遠不會應用 CORS 標頭配置。
這是.php文件中的所有代碼
<?php
$url = 'someurl';
$fields = ['pax' => '2', 'ownerid' => '1', 'channel' => '3'];
//url-ify the data for the POST
$fields_string = http_build_query($fields);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//So that curl_exec returns the contents of the cURL; rather than echoing it
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//Set Headers
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization': 'someAuth',
'x-api-key': 'someKey',
'Content-Type': 'application/x-www-form-urlencoded',
));
//DISABLE SSL
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//execute post
$result = curl_exec($ch);
if($result === false){
echo 'Curl error: ' . curl_error($ch);
}else{
//$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//CORS
if (isset($_SERVER['HTTP_ORIGIN'])) {
//header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header("Access-Control-Allow-Origin: http://localhost:4200");
header('Access-Control-Allow-Credentials: true');
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
}
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers:{$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
echo $result;
};
curl_close($ch);
?>
在curl請求中設置一些標題不會更改.php腳本中的標題。
更新1:
您的php腳本中有一個解析錯誤(您自己發現):
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization': 'someAuthorization',
'x-api-key': 'somekey',
'Content-Type': 'application/x-www-form-urlencoded'
));
應該:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: someAuthorization',
'x-api-key: somekey',
'Content-Type: application/x-www-form-urlencoded'
));
對該腳本的Ajax調用會導致錯誤500響應,該響應具有默認標頭, 而不包含您嘗試在.php腳本中設置的標頭。
在您的本地環境中啟用錯誤報告以查看問題“較早”。
例如在php.ini中:
error_reporting=E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
display_errors=On
@see 如何獲取顯示的PHP錯誤?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.