{"id":48860,"date":"2024-12-02T13:53:10","date_gmt":"2024-12-02T05:53:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48860\/"},"modified":"2024-12-02T13:53:10","modified_gmt":"2024-12-02T05:53:10","slug":"postman-%e8%b0%83%e7%94%a8%e6%9c%ac%e5%9c%b0-php-api-%e6%97%b6%ef%bc%8c%e4%b8%ba%e4%bd%95%e6%89%be%e4%b8%8d%e5%88%b0-_serverhttp_origin%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48860\/","title":{"rendered":"Postman \u8c03\u7528\u672c\u5730 PHP API \u65f6\uff0c\u4e3a\u4f55\u627e\u4e0d\u5230 $_SERVER[\\&#8217;HTTP_ORIGIN\\&#8217;]\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Postman \u8c03\u7528\u672c\u5730 PHP API \u65f6\uff0c\u4e3a\u4f55\u627e\u4e0d\u5230 $_SERVER[\\&#8217;HTTP_ORIGIN\\&#8217;]\uff1f<\/h1>\n<p><span style=\"font-size: 15px\">\u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u7c73\u4e91\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPostman \u8c03\u7528\u672c\u5730 PHP API \u65f6\uff0c\u4e3a\u4f55\u627e\u4e0d\u5230 $_SERVER[\\&#8217;HTTP_ORIGIN\\&#8217;]\uff1f\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241101\/17304723386724e9925592e.jpg\" class=\"aligncenter\" title=\"Postman \u8c03\u7528\u672c\u5730 PHP API \u65f6\uff0c\u4e3a\u4f55\u627e\u4e0d\u5230 $_SERVER[\\&#8217;HTTP_ORIGIN\\&#8217;]\uff1f\u63d2\u56fe\" alt=\"Postman \u8c03\u7528\u672c\u5730 PHP API \u65f6\uff0c\u4e3a\u4f55\u627e\u4e0d\u5230 $_SERVER[\\&#8217;HTTP_ORIGIN\\&#8217;]\uff1f\u63d2\u56fe\" \/><\/p>\n<h2>postman \u8c03\u7528 localhost php api \u65f6\u4e3a\u4f55\u627e\u4e0d\u5230 $_server[&#8216;http_origin&#8217;]\uff1f<\/h2>\n<p>postman \u8c03\u7528\u672c\u5730 php api \u65f6\u51fa\u73b0\u201cnotice: undefined index: http_origin\u201d\u9519\u8bef\uff0c\u662f\u56e0\u4e3a origin \u5934\u662f\u6d4f\u89c8\u5668 cors \u4e2d\u7528\u6765\u533a\u5206\u8bf7\u6c42\u6765\u6e90\u7684\uff0c\u800c\u5176\u4ed6\u73af\u5883\u4e0d\u53d7\u5176\u9650\u5236\u3002<\/p>\n<h3>postman \u6a21\u62df http_origin<\/h3>\n<p>\u8981\u6a21\u62df origin \u5934\uff0c\u5728 postman \u7684 headers \u6807\u7b7e\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a origin \u7684\u81ea\u5b9a\u4e49\u5934\uff0c\u5e76\u5c06\u503c\u8bbe\u7f6e\u4e3a\u7f51\u7ad9\u7684 url\uff08\u683c\u5f0f\u4e3a &lt;schema&gt;:\/\/&lt;host&gt;[:&lt;port=80&gt;]\uff09\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e https:\/\/example.com\uff0corigin \u503c\u5e94\u4e3a https:\/\/example.com\u3002<\/p>\n<h3>\u6ce8\u610f<\/h3>\n<p>\u7531\u4e8e\u4e0d\u662f\u6240\u6709\u7528\u6237\u4ee3\u7406\u90fd\u4f1a\u53d1\u9001 origin \u5934\uff0c\u56e0\u6b64\u5728 php \u4e2d\u5e94\u4f7f\u7528 isset() \u6216 ?? \u64cd\u4f5c\u7b26\u68c0\u67e5\u5176\u662f\u5426\u5b58\u5728\u540e\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>\u672c\u7bc7\u5173\u4e8e\u300aPostman \u8c03\u7528\u672c\u5730 PHP API \u65f6\uff0c\u4e3a\u4f55\u627e\u4e0d\u5230 $_SERVER[\\&#8217;HTTP_ORIGIN\\&#8217;]\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Postman \u8c03\u7528\u672c\u5730 PHP API \u65f6\uff0c\u4e3a\u4f55\u627e\u4e0d\u5230 $_SERVER[\\&#8217;HTTP_ORIGIN\\&#8217;]\uff1f \u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u7c73\u4e91\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765\u300aPostman \u8c03\u7528\u672c\u5730 PHP API \u65f6\uff0c\u4e3a\u4f55\u627e\u4e0d\u5230 $_SERVER[\\&#8217;HTTP_ORIGIN\\&#8217;]\uff1f\u300b\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60\u6587\u7ae0\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01 postman \u8c03\u7528 localhost php api \u65f6\u4e3a\u4f55\u627e\u4e0d\u5230 $_server[&#8216;http_origin&#8217;]\uff1f postman \u8c03\u7528\u672c\u5730 php api \u65f6\u51fa\u73b0\u201cnotice: undefined index: http_origin\u201d\u9519\u8bef\uff0c\u662f\u56e0\u4e3a origin \u5934\u662f\u6d4f\u89c8\u5668 cors \u4e2d\u7528\u6765\u533a\u5206\u8bf7\u6c42\u6765\u6e90\u7684\uff0c\u800c\u5176\u4ed6\u73af\u5883\u4e0d\u53d7\u5176\u9650\u5236\u3002 postman \u6a21\u62df http_origin \u8981\u6a21\u62df origin \u5934\uff0c\u5728 postman \u7684 headers \u6807\u7b7e\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a origin \u7684\u81ea\u5b9a\u4e49\u5934\uff0c\u5e76\u5c06\u503c\u8bbe\u7f6e\u4e3a\u7f51\u7ad9\u7684 url\uff08\u683c\u5f0f\u4e3a &lt;schema&gt;:\/\/&lt;host&gt;[:&lt;port=80&gt;]\uff09\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e https:\/\/example.com\uff0corigin \u503c\u5e94\u4e3a https:\/\/example.com\u3002 \u6ce8\u610f \u7531\u4e8e\u4e0d\u662f\u6240\u6709\u7528\u6237\u4ee3\u7406\u90fd\u4f1a\u53d1\u9001 origin \u5934\uff0c\u56e0\u6b64\u5728 php \u4e2d\u5e94\u4f7f\u7528 isset() [&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-48860","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48860","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=48860"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48860\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}