{"id":48585,"date":"2024-12-02T11:59:32","date_gmt":"2024-12-02T03:59:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48585\/"},"modified":"2024-12-02T11:59:32","modified_gmt":"2024-12-02T03:59:32","slug":"%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%e4%bd%bf%e7%94%a8-postman-%e8%b0%83%e7%94%a8-localhost-php-api-%e6%97%b6%e4%b8%a2%e5%a4%b1-_serverhttp_origin-%e7%9a%84%e9%97%ae%e9%a2%98%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48585\/","title":{"rendered":"\u5982\u4f55\u89e3\u51b3\u4f7f\u7528 Postman \u8c03\u7528 localhost PHP API \u65f6\u4e22\u5931 $_SERVER[\\&#8217;HTTP_ORIGIN\\&#8217;] \u7684\u95ee\u9898\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u89e3\u51b3\u4f7f\u7528 Postman \u8c03\u7528 localhost PHP API \u65f6\u4e22\u5931 $_SERVER[\\&#8217;HTTP_ORIGIN\\&#8217;] \u7684\u95ee\u9898\uff1f<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u89e3\u51b3\u4f7f\u7528 Postman \u8c03\u7528 localhost PHP API \u65f6\u4e22\u5931 $_SERVER[\\&#8217;HTTP_ORIGIN\\&#8217;] \u7684\u95ee\u9898\uff1f\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241028\/1730099928671f3ad846f27.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u89e3\u51b3\u4f7f\u7528 Postman \u8c03\u7528 localhost PHP API \u65f6\u4e22\u5931 $_SERVER[\\&#8217;HTTP_ORIGIN\\&#8217;] \u7684\u95ee\u9898\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u89e3\u51b3\u4f7f\u7528 Postman \u8c03\u7528 localhost PHP API \u65f6\u4e22\u5931 $_SERVER[\\&#8217;HTTP_ORIGIN\\&#8217;] \u7684\u95ee\u9898\uff1f\u63d2\u56fe\" \/><\/p>\n<h2>\u4f7f\u7528 postman \u6253 localhost php api \u65f6\u4e22\u5931 $_server[&#8216;http_origin&#8217;] \u7684\u89e3\u51b3\u65b9\u6cd5<\/h2>\n<p>\u201corigin\u201d\u662f\u6d4f\u89c8\u5668 cors \u4e2d\u7528\u6765\u533a\u5206\u6765\u6e90\u7684 header \u5934\u3002\u8be5\u5934\u4ec5\u5728\u6d4f\u89c8\u5668\u73af\u5883\u4e2d\u4f7f\u7528\uff0c\u5728\u5176\u4ed6\u73af\u5883\uff08\u5982 php\uff09\u4e2d\u5e76\u4e0d\u53d7 cors \u9650\u5236\u3002\u56e0\u6b64\uff0c\u5728 postman \u4e2d\u8c03\u7528 localhost php api \u65f6\uff0c\u53ef\u80fd\u4e0d\u4f1a\u53d1\u9001\u6b64\u5934\uff0c\u5bfc\u81f4 php \u811a\u672c\u4e2d $_server[&#8216;http_origin&#8217;] \u65e0\u6cd5\u83b7\u53d6\u3002<\/p>\n<h2>\u5728 postman \u4e2d\u6a21\u62df http_origin<\/h2>\n<p>\u4ee5\u4e0b\u662f\u5728 postman \u4e2d\u6a21\u62df http_origin \u7684\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u5728 postman \u4e2d\u6253\u5f00\u9700\u8981\u8c03\u7528\u7684 api \u8bf7\u6c42\u3002<\/li>\n<li>\u5207\u6362\u5230\u201cheader\u201d\u9009\u9879\u5361\u3002<\/li>\n<li>\u5728\u540d\u79f0\u680f\u4e2d\u8f93\u5165\u201corigin\u201d\u3002<\/li>\n<li>\u5728\u503c\u680f\u4e2d\u8f93\u5165 api \u6765\u6e90\u7684 url\uff0c\u683c\u5f0f\u4e3a\uff1a&lt;schema&gt;:\/\/&lt;host&gt;[:&lt;port=80&gt;]\u3002\u4f8b\u5982\uff1ahttps:\/\/example.com<\/li>\n<\/ol>\n<h2>\u6ce8\u610f\uff1a<\/h2>\n<p>\u7531\u4e8e http_origin \u5e76\u975e\u6240\u6709\u7528\u6237\u4ee3\u7406\u90fd\u4f1a\u53d1\u9001\uff0c\u56e0\u6b64\u5728 php \u4e2d\uff0c\u9700\u8981\u4f7f\u7528 isset \u6216 ?? \u64cd\u4f5c\u7b26\u5224\u65ad\u8be5\u5934\u662f\u5426\u5b58\u5728\uff0c\u518d\u53d6\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>$origin = $_server['http_origin'] ?? '';<\/pre>\n<p>\u6216<\/p>\n<pre>$origin = '';\nif(isset($_SERVER['HTTP_ORIGIN'])){\n  $origin = $_SERVER['HTTP_ORIGIN'];\n}<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u89e3\u51b3\u4f7f\u7528 Postman \u8c03\u7528 localhost PHP API \u65f6\u4e22\u5931 $_SERVER[\\&#8217;HTTP_ORIGIN\\&#8217;] \u7684\u95ee\u9898\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u89e3\u51b3\u4f7f\u7528 Postman \u8c03\u7528 localhost PHP API \u65f6\u4e22\u5931 $_SERVER[\\&#8217;HTTP_ORIGIN\\&#8217;] \u7684\u95ee\u9898\uff1f \u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60\u6587\u7ae0\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765\u300a\u5982\u4f55\u89e3\u51b3\u4f7f\u7528 Postman \u8c03\u7528 localhost PHP API \u65f6\u4e22\u5931 $_SERVER[\\&#8217;HTTP_ORIGIN\\&#8217;] \u7684\u95ee\u9898\uff1f\u300b\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01 \u4f7f\u7528 postman \u6253 localhost php api \u65f6\u4e22\u5931 $_server[&#8216;http_origin&#8217;] \u7684\u89e3\u51b3\u65b9\u6cd5 \u201corigin\u201d\u662f\u6d4f\u89c8\u5668 cors \u4e2d\u7528\u6765\u533a\u5206\u6765\u6e90\u7684 header \u5934\u3002\u8be5\u5934\u4ec5\u5728\u6d4f\u89c8\u5668\u73af\u5883\u4e2d\u4f7f\u7528\uff0c\u5728\u5176\u4ed6\u73af\u5883\uff08\u5982 php\uff09\u4e2d\u5e76\u4e0d\u53d7 cors \u9650\u5236\u3002\u56e0\u6b64\uff0c\u5728 postman \u4e2d\u8c03\u7528 localhost php api \u65f6\uff0c\u53ef\u80fd\u4e0d\u4f1a\u53d1\u9001\u6b64\u5934\uff0c\u5bfc\u81f4 php \u811a\u672c\u4e2d $_server[&#8216;http_origin&#8217;] \u65e0\u6cd5\u83b7\u53d6\u3002 \u5728 postman \u4e2d\u6a21\u62df http_origin \u4ee5\u4e0b\u662f\u5728 postman \u4e2d\u6a21\u62df http_origin \u7684\u6b65\u9aa4\uff1a \u5728 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-48585","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48585","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=48585"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48585\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}