{"id":2577,"date":"2023-06-24T22:44:46","date_gmt":"2023-06-24T14:44:46","guid":{"rendered":"https:\/\/www.tra56.com\/?p=2577"},"modified":"2023-06-24T22:44:46","modified_gmt":"2023-06-24T14:44:46","slug":"2577","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/2577.html","title":{"rendered":"PHP\u5982\u4f55\u5229\u7528curl\u53d1\u9001HTTP\u8bf7\u6c42"},"content":{"rendered":"

\u8fd9\u7bc7\u6587\u7ae0\u5c06\u4e3a\u5927\u5bb6\u8be6\u7ec6\u8bb2\u89e3\u6709\u5173PHP<\/a>\u5982\u4f55\u5229\u7528curl<\/a>\u53d1\u9001HTTP\u8bf7\u6c42<\/a>\uff0c\u5c0f\u7f16\u89c9\u5f97\u633a\u5b9e\u7528\u7684\uff0c\u56e0\u6b64\u5206\u4eab\u7ed9\u5927\u5bb6\u505a\u4e2a\u53c2\u8003\uff0c\u5e0c\u671b\u5927\u5bb6\u9605\u8bfb\u5b8c\u8fd9\u7bc7\u6587\u7ae0\u540e\u53ef\u4ee5\u6709\u6240\u6536\u83b7\u3002<\/p>\n

cURL \u51fd\u6570\u6982\u8ff0<\/strong><\/p>\n

PHP\u652f\u6301\u7684\u7531Daniel Stenberg\u521b\u5efa\u7684libcurl\u5e93\u5141\u8bb8\u4f60\u4e0e\u5404\u79cd\u7684\u670d\u52a1\u5668\u4f7f\u7528\u5404\u79cd\u7c7b\u578b\u7684\u534f\u8bae\u8fdb\u884c\u8fde\u63a5\u548c\u901a\u8baf\u3002<\/p>\n

libcurl\u76ee\u524d\u652f\u6301http\u3001https\u3001ftp\u3001gopher\u3001telnet\u3001dict\u3001file\u548cldap\u534f\u8bae\u3002libcurl\u540c\u65f6\u4e5f\u652f\u6301HTTPS\u8ba4\u8bc1\u3001HTTP POST\u3001HTTP PUT\u3001 FTP \u4e0a\u4f20(\u8fd9\u4e2a\u4e5f\u80fd\u901a\u8fc7PHP\u7684FTP\u6269\u5c55\u5b8c\u6210)\u3001HTTP \u57fa\u4e8e\u8868\u5355\u7684\u4e0a\u4f20\u3001\u4ee3\u7406\u3001cookies\u548c\u7528\u6237\u540d+\u5bc6\u7801\u7684\u8ba4\u8bc1\u3002<\/p>\n

PHP\u4e2d\u4f7f\u7528cURL\u5b9e\u73b0Get\u548cPost\u8bf7\u6c42\u7684\u65b9\u6cd5<\/p>\n

\u8fd9\u4e9b\u51fd\u6570\u5728PHP 4.0.2\u4e2d\u88ab\u5f15\u5165\u3002<\/p>\n

\u5b9e\u4f8b<\/strong><\/p>\n

\u56e0\u4e3a\u9700\u8981\u5728 php \u5f00\u53d1\u4e2d\u5bf9\u63a5\u5176\u5b83\u63a5\u53e3\u9700\u8981\u7528 php curl \u53bb\u5bf9\u63a5\u5176\u5b83\u63a5\u53e3 \u6211\u628a\u4ed6\u4eec\u5c01\u88c5\u6210\u51fd\u6570 \u5e0c\u671b\u80fd\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n

\u8fd9\u91cc\u9762\u662f\u5c01\u88c5\u597d\u7684\u4f1a\u81ea\u52a8\u628a data \u8fdb\u884c\u8f6c\u6210 json \u683c\u5f0f\uff0c\u540c\u65f6\u89e3\u7801\u6210 php \u6570\u7ec4\u8f93\u51fa\u3002<\/p>\n

<?php\r\nfunction geturl($url){\r\n    $headerArray =array("Content-type:application\/json;","Accept:application\/json");\r\n    $ch = curl_init();\r\n    curl_setopt($ch, CURLOPT_URL, $url);\r\n    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); \r\n    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); \r\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\r\n    curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);\r\n    $output = curl_exec($ch);\r\n    curl_close($ch);\r\n    $output = json_decode($output,true);\r\n    return $output;\r\n}\r\n \r\n \r\nfunction posturl($url,$data){\r\n    $data = json_encode($data);  \r\n    $headerArray =array("Content-type:application\/json;charset='utf-8'","Accept:application\/json");\r\n    $curl = curl_init();\r\n    curl_setopt($curl, CURLOPT_URL, $url);\r\n    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);\r\n    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);\r\n    curl_setopt($curl, CURLOPT_POST, 1);\r\n    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);\r\n    curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);\r\n    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);\r\n    $output = curl_exec($curl);\r\n    curl_close($curl);\r\n    return json_decode($output\uff0ctrue);\r\n}\r\n \r\n \r\nfunction puturl($url,$data){\r\n  $data = json_encode($data);\r\n  $ch = curl_init(); \/\/\u521d\u59cb\u5316CURL\u53e5\u67c4 \r\n  curl_setopt($ch, CURLOPT_URL, $url); \/\/\u8bbe\u7f6e\u8bf7\u6c42\u7684URL\r\n  curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type:application\/json'));\r\n  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); \/\/\u8bbe\u4e3aTRUE\u628acurl_exec()\u7ed3\u679c\u8f6c\u5316\u4e3a\u5b57\u4e32\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u8f93\u51fa \r\n  curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PUT"); \/\/\u8bbe\u7f6e\u8bf7\u6c42\u65b9\u5f0f\r\n  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);\/\/\u8bbe\u7f6e\u63d0\u4ea4\u7684\u5b57\u7b26\u4e32\r\n  $output = curl_exec($ch);\r\n  curl_close($ch);\r\n  return json_decode($output,true);\r\n}\r\n \r\nfunction delurl($url,$data){\r\n  $data = json_encode($data);\r\n  $ch = curl_init();\r\n  curl_setopt ($ch,CURLOPT_URL,$put_url);\r\n  curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type:application\/json'));\r\n  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);\r\n  curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE");  \r\n  curl_setopt($ch, CURLOPT_POSTFIELDS,$data);\r\n  $output = curl_exec($ch);\r\n  curl_close($ch);\r\n  $output = json_decode($output,true);\r\n}\r\n \r\nfunction patchurl($url,$data){\r\n  $data = json_encode($data);\r\n  $ch = curl_init();\r\n  curl_setopt ($ch,CURLOPT_URL,$url);\r\n  curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type:application\/json'));\r\n  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);\r\n  curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PATCH"); \r\n  curl_setopt($ch, CURLOPT_POSTFIELDS,$data);   \/\/20170611\u4fee\u6539\u63a5\u53e3\uff0c\u7528\/id\u7684\u65b9\u5f0f\u4f20\u9012\uff0c\u76f4\u63a5\u5199\u5728url\u4e2d\u4e86\r\n  $output = curl_exec($ch);\r\n  curl_close($ch);\r\n  $output = json_decode($output);\r\n  return $output;\r\n}\r\n?><\/pre>\n

<\/p>\n","protected":false},"excerpt":{"rendered":"

\u8fd9\u7bc7\u6587\u7ae0\u5c06\u4e3a\u5927\u5bb6\u8be6\u7ec6\u8bb2\u89e3\u6709\u5173PHP\u5982\u4f55\u5229\u7528curl\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u5c0f\u7f16\u89c9\u5f97\u633a\u5b9e\u7528\u7684\uff0c\u56e0\u6b64\u5206\u4eab\u7ed9\u5927\u5bb6\u505a\u4e2a\u53c2\u8003\uff0c […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,92],"tags":[],"class_list":["post-2577","post","type-post","status-publish","format-standard","hentry","category-php","category-phpcode"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2577"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=2577"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2577\/revisions"}],"predecessor-version":[{"id":2578,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2577\/revisions\/2578"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=2577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=2577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=2577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}