{"id":1428,"date":"2021-09-27T23:55:20","date_gmt":"2021-09-27T15:55:20","guid":{"rendered":"https:\/\/www.siediyer.cn\/?p=1428"},"modified":"2021-09-27T23:55:20","modified_gmt":"2021-09-27T15:55:20","slug":"php%e5%8f%91%e9%80%81http-put-patch-delete%e8%af%b7%e6%b1%82","status":"publish","type":"post","link":"https:\/\/www.siediyer.cn\/?p=1428","title":{"rendered":"php\u53d1\u9001http put\/patch\/delete\u8bf7\u6c42"},"content":{"rendered":"<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;?php\r\n\/**\r\n * http.php\r\n * \u7528\u6765\u5411\u670d\u52a1\u5668\u7684RESTful API\u53d1\u8d77\u5404\u7c7bHTTP\u8bf7\u6c42\u7684\u5de5\u5177\u51fd\u6570\u3002\r\n *\r\n * \u4f7f\u7528: http:\/\/mysite.com\/http.php?action=xxx\r\n * xxx \\in {get,post,put,patch,delete}\r\n *\r\n * Created by PhpStorm.\r\n * User: chris\r\n * Date: 16\/8\/11\r\n * Time: \u4e0b\u53481:22\r\n *\/\r\n\r\nclass commonFunction{\r\n    function callInterfaceCommon($URL,$type,$params,$headers){\r\n        $ch = curl_init($URL);\r\n        $timeout = 5;\r\n        if($headers!=\"\"){\r\n            curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);\r\n        }else {\r\n            curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type: application\/json'));\r\n        }\r\n        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);\r\n        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);\r\n        switch ($type){\r\n            case \"GET\" : curl_setopt($ch, CURLOPT_HTTPGET, true);break;\r\n            case \"POST\": curl_setopt($ch, CURLOPT_POST,true);\r\n                curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break;\r\n            case \"PUT\" : curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, \"PUT\");\r\n                curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break;\r\n            case \"PATCH\": curl_setopt($ch, CULROPT_CUSTOMREQUEST, 'PATCH');\r\n                curl_setopt($ch, CURLOPT_POSTFIELDS, $params);break;\r\n            case \"DELETE\":curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, \"DELETE\");\r\n                curl_setopt($ch, CURLOPT_POSTFIELDS,$params);break;\r\n        }\r\n        $file_contents = curl_exec($ch);\/\/\u83b7\u5f97\u8fd4\u56de\u503c\r\n        return $file_contents;\r\n        curl_close($ch);\r\n    }\r\n}\r\n\r\n\r\n$params=\"{user:\\\"admin\\\",pwd:\\\"admin\\\"}\";\r\n\/\/$headers=array('Content-Type: text\/html; charset=utf-8');\r\n\/\/$headers=array('accept: application\/json; Content-Type:application\/json-patch+json');\r\n$headers=array('Content-Type:application\/json-patch+json');\r\n#$url=$GLOBALS[\"serviceUrl\"].\"\/user\";\r\n$url='http:\/\/mysite.com\/user\/11';\r\n$cf = new commonFunction();\r\n\r\n$action=strtoupper($_GET['action']);\r\necho \"\u4f60\u6307\u5b9a\u7684HTTP\u8bf7\u6c42\u52a8\u4f5c\u4e3a\".$action.\"&lt;br\/&gt;&lt;hr\/&gt;\";\r\n\r\n$strResult = $cf-&gt;callInterfaceCommon($url,$action,$params,$headers);\r\necho \"\u6267\u884c\u8be5HTTP\u8bf7\u6c42\u52a8\u4f5c,\u5f97\u5230&lt;br\/&gt;\".$strResult;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt;?php \/** * http.php * \u7528\u6765\u5411\u670d\u52a1\u5668\u7684RESTful API\u53d1\u8d77\u5404\u7c7bHTTP\u8bf7\u6c42\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1428","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/1428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1428"}],"version-history":[{"count":1,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/1428\/revisions"}],"predecessor-version":[{"id":1429,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/1428\/revisions\/1429"}],"wp:attachment":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}