{"id":1060,"date":"2015-07-10T17:36:21","date_gmt":"2015-07-10T09:36:21","guid":{"rendered":"http:\/\/www.poloo.org\/?p=1060"},"modified":"2015-07-10T17:36:21","modified_gmt":"2015-07-10T09:36:21","slug":"%e7%94%a8php%e5%8f%91%e9%80%81http%e8%af%b7%e6%b1%82%ef%bc%88post%e8%af%b7%e6%b1%82%e3%80%81get%e8%af%b7%e6%b1%82%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=1060","title":{"rendered":"\u7528PHP\u53d1\u9001HTTP\u8bf7\u6c42\uff08POST\u8bf7\u6c42\u3001GET\u8bf7\u6c42\uff09"},"content":{"rendered":"<p>php\u7684http\u8bf7\u6c42\u6709\u5f88\u591a\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528 \u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528curl \u6269\u5c55\uff08php\u914d\u7f6e\u4e2d\u9700\u8981\u542f\u7528curl\u6269\u5c55\uff09<\/p>\n<p><strong>file_get_contents\u7248\u672c\uff1a<\/strong><\/p>\n<pre class=\"brush:php\">&lt;?php\r\n\/**\r\n * \u53d1\u9001post\u8bf7\u6c42\r\n * @param string $url \u8bf7\u6c42\u5730\u5740\r\n * @param array $post_data post\u952e\u503c\u5bf9\u6570\u636e\r\n * @return string\r\n *\/\r\nfunction send_post($url, $post_data) {\r\n \r\n    $postdata = http_build_query($post_data);\r\n    $options = array(\r\n        'http' =&gt; array(\r\n            'method' =&gt; 'POST',\r\n            'header' =&gt; 'Content-type:application\/x-www-form-urlencoded',\r\n            'content' =&gt; $postdata,\r\n            'timeout' =&gt; 15 * 60 \/\/ \u8d85\u65f6\u65f6\u95f4\uff08\u5355\u4f4d:s\uff09\r\n        )\r\n    );\r\n    $context = stream_context_create($options);\r\n    $result = file_get_contents($url, false, $context);\r\n \r\n    return $result;\r\n}\r\n<\/pre>\n<p><strong>\u4f7f\u7528\u5982\u4e0b\uff1a<\/strong><\/p>\n<pre class=\"brush:php\">$post_data = array(\r\n\t'username' =&gt; 'xxx',\r\n\t'password' =&gt; '000'\r\n);\r\nsend_post('http:\/\/blog.sohu.com', $post_data);<\/pre>\n<p>PS: \u5728\u53d1\u9001http\u8bf7\u6c42\u7684\u65f6\u5019\uff0c\u5982\u679c\u670d\u52a1\u5668\u5904\u7406\u8bf7\u6c42\u65f6\u95f4\u8d85\u957f\uff0c\u672c\u5730php\u4f1a\u4e2d\u65ad\u8bf7\u6c42\uff08\u8d85\u65f6\u4e2d\u65ad\uff09\u9700\u8981\u628a http \u8bf7\u6c42\u65f6\u95f4\u9650\u5236\u8bbe\u7f6e\u5927\u4e00\u4e9b\uff08timeout\uff09<\/p>\n<p><strong>Socket\u7248\u672c:<\/strong><\/p>\n<pre class=\"brush:php\">\/**\r\n * Socket\u7248\u672c\r\n * \u4f7f\u7528\u65b9\u6cd5\uff1a\r\n * $post_string = \"app=socket&amp;amp;version=beta\";\r\n * request_by_socket('blog.sohu.com', '\/restServer.php', $post_string);\r\n *\/\r\nfunction request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30) {\r\n\t$socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);\r\n\tif (!$socket) die(\"$errstr($errno)\");\r\n\tfwrite($socket, \"POST $remote_path HTTP\/1.0\");\r\n\tfwrite($socket, \"User-Agent: Socket Example\");\r\n\tfwrite($socket, \"HOST: $remote_server\");\r\n\tfwrite($socket, \"Content-type: application\/x-www-form-urlencoded\");\r\n\tfwrite($socket, \"Content-length: \" . \uff08strlen($post_string) + 8\uff09 . \"\");\r\n\tfwrite($socket, \"Accept:*\/*\");\r\n\tfwrite($socket, \"\");\r\n\tfwrite($socket, \"mypost=$post_string\");\r\n\tfwrite($socket, \"\");\r\n\t$header = \"\";\r\n\twhile ($str = trim(fgets($socket, 4096))) {\r\n\t\t$header .= $str;\r\n\t}\r\n\r\n\t$data = \"\";\r\n\twhile (!feof($socket)) {\r\n\t\t$data .= fgets($socket, 4096);\r\n\t}\r\n\r\n\treturn $data;\r\n}<\/pre>\n<p><strong>Curl\u7248\u672c:<\/strong><\/p>\n<pre class=\"brush:php\">\/**\r\n * Curl\u7248\u672c\r\n * \u4f7f\u7528\u65b9\u6cd5\uff1a\r\n * $post_string = \"app=request&amp;version=beta\";\r\n * request_by_curl('http:\/\/blog.sohu.com\/restServer.php', $post_string);\r\n *\/\r\nfunction request_by_curl($remote_server, $post_string) {\r\n\t$ch = curl_init();\r\n\tcurl_setopt($ch, CURLOPT_URL, $remote_server);\r\n\tcurl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);\r\n\tcurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\r\n\tcurl_setopt($ch, CURLOPT_USERAGENT, \"sohu.com's CURL Example beta\");\r\n\t$data = curl_exec($ch);\r\n\tcurl_close($ch);\r\n\r\n\treturn $data;\r\n}<\/pre>\n<p><strong>Curl\u7248\u672c(2)<\/strong><\/p>\n<pre class=\"brush:php\">\/**\r\n * \u53d1\u9001HTTP\u8bf7\u6c42\r\n *\r\n * @param string $url \u8bf7\u6c42\u5730\u5740\r\n * @param string $method \u8bf7\u6c42\u65b9\u5f0f GET\/POST\r\n * @param string $refererUrl \u8bf7\u6c42\u6765\u6e90\u5730\u5740\r\n * @param array $data \u53d1\u9001\u6570\u636e\r\n * @param string $contentType \r\n * @param string $timeout\r\n * @param string $proxy\r\n * @return boolean\r\n *\/\r\nfunction send_request($url, $data, $refererUrl = '', $method = 'GET', $contentType = 'application\/json', $timeout = 30, $proxy = false) {\r\n\t$ch = null;\r\n\tif('POST' === strtoupper($method)) {\r\n\t\t$ch = curl_init($url);\r\n\t\tcurl_setopt($ch, CURLOPT_POST, 1);\r\n\t\tcurl_setopt($ch, CURLOPT_HEADER,0 );\r\n\t\tcurl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);\r\n\t\tcurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\r\n\t\tcurl_setopt($ch, CURLOPT_FORBID_REUSE, 1);\r\n\t\tcurl_setopt($ch, CURLOPT_TIMEOUT, $timeout);\r\n\t\tif ($refererUrl) {\r\n\t\t\tcurl_setopt($ch, CURLOPT_REFERER, $refererUrl);\r\n\t\t}\r\n\t\tif($contentType) {\r\n\t\t\tcurl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType));\r\n\t\t}\r\n\t\tif(is_string($data)){\r\n\t\t\tcurl_setopt($ch, CURLOPT_POSTFIELDS, $data);\r\n\t\t} else {\r\n\t\t\tcurl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));\r\n\t\t}\r\n\t} else if('GET' === strtoupper($method)) {\r\n\t\tif(is_string($data)) {\r\n\t\t\t$real_url = $url. (strpos($url, '?') === false ? '?' : ''). $data;\r\n\t\t} else {\r\n\t\t\t$real_url = $url. (strpos($url, '?') === false ? '?' : ''). http_build_query($data);\r\n\t\t}\r\n\r\n\t\t$ch = curl_init($real_url);\r\n\t\tcurl_setopt($ch, CURLOPT_HEADER, 0);\r\n\t\tcurl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType));\r\n\t\tcurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\r\n\t\tcurl_setopt($ch, CURLOPT_TIMEOUT, $timeout);\r\n\t\tif ($refererUrl) {\r\n\t\t\tcurl_setopt($ch, CURLOPT_REFERER, $refererUrl);\r\n\t\t}\r\n\t} else {\r\n\t\t$args = func_get_args();\r\n\t\treturn false;\r\n\t}\r\n\r\n\tif($proxy) {\r\n\t\tcurl_setopt($ch, CURLOPT_PROXY, $proxy);\r\n\t}\r\n\t$ret = curl_exec($ch);\r\n\t$info = curl_getinfo($ch);\r\n\t$contents = array(\r\n\t\t\t'httpInfo' =&gt; array(\r\n\t\t\t\t\t'send' =&gt; $data,\r\n\t\t\t\t\t'url' =&gt; $url,\r\n\t\t\t\t\t'ret' =&gt; $ret,\r\n\t\t\t\t\t'http' =&gt; $info,\r\n\t\t\t)\r\n\t);\r\n\r\n\tcurl_close($ch);\r\n\treturn $ret;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u539f\u6587\uff1ahttp:\/\/blog.snsgou.com\/post-161.html<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php\u7684http\u8bf7\u6c42\u6709\u5f88\u591a\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528 \u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[173,172],"class_list":["post-1060","post","type-post","status-publish","format-standard","hentry","category-Php","tag-curl","tag-http"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/1060","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1060"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/1060\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}