{"id":1111,"date":"2014-11-27T20:48:47","date_gmt":"2014-11-27T12:48:47","guid":{"rendered":"http:\/\/www.awaysoft.com\/taor\/?p=1111"},"modified":"2014-11-27T09:54:46","modified_gmt":"2014-11-27T01:54:46","slug":"php%e4%bd%bf%e7%94%a8fsockopen%e6%8c%87%e5%ae%9aip%e9%80%9a%e8%bf%87get%e6%88%96%e8%80%85post%e8%8e%b7%e5%8f%96%e8%bf%9c%e7%a8%8b%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/actom.me\/blog\/php%e4%bd%bf%e7%94%a8fsockopen%e6%8c%87%e5%ae%9aip%e9%80%9a%e8%bf%87get%e6%88%96%e8%80%85post%e8%8e%b7%e5%8f%96%e8%bf%9c%e7%a8%8b%e6%95%b0%e6%8d%ae.html","title":{"rendered":"PHP\u4f7f\u7528fsockopen\u6307\u5b9aip\u901a\u8fc7GET\u6216\u8005POST\u83b7\u53d6\u8fdc\u7a0b\u6570\u636e"},"content":{"rendered":"<p>\u5f53\u57df\u540d\u4e0eip\u4e0d\u76f8\u7b26\uff0c\u53c8\u65e0\u6cd5\u4fee\u6539\u7cfb\u7edfhosts\u65f6\uff0c\u6211\u4eec\u9700\u8981\u6307\u5b9aip\u6765\u83b7\u53d6\u8fdc\u7a0b\u6570\u636e\uff0c\u901a\u8fc7\u641c\u7d22php curl\u76f8\u5173\u8d44\u6599\uff0c\u672a\u53d1\u73b0\u53ef\u7528\u65b9\u6cd5\uff0c\u6545\u5199\u4e86\u5982\u4e0b\u4e24\u4e2a\u51fd\u6570\u3002<\/p>\n<p>\u51fd\u6570\u652f\u6301get\u6216\u8005post\u65b9\u5f0f\uff0c301\u81ea\u52a8\u8df3\u8f6c\uff0c\u652f\u6301\u81ea\u5b9a\u4e49header\uff0c\u4ec5\u652f\u6301ipv4\u7684http\u534f\u8bae\u3002<\/p>\n<p>\u4ee3\u7801\u5df2\u96c6\u6210\u5230<a href=\"https:\/\/github.com\/awaysoft\/AlonePHP\" target=\"_blank\">AlonePHP<\/a>\u4e2d\u3002<\/p>\n<pre class=\"lang:php decode:true \">\/* http_get_with_ip,\u7528\u4e8eip\u89e3\u6790\u9519\u8bef\u6216\u8005\u9700\u8981\u6307\u5b9aip\u7684\u65f6\u5019\u4ee5get\u65b9\u5f0f\u83b7\u53d6\u6570\u636e\r\n * @param string $url \u8fdc\u7a0b\u5730\u5740\r\n * @param string $ip \u9700\u8981\u8bbf\u95ee\u7684ip\r\n * @param array $header \u9700\u8981\u6dfb\u52a0\u7684\u5176\u5b83header\u53c2\u6570\r\n * @return string|boolean \u8fd4\u56de\u83b7\u53d6\u7684\u5185\u5bb9\uff0c\u8bbf\u95ee\u5931\u8d25\u8fd4\u56defalse\r\n *\/\r\nfunction http_get_with_ip($url, $ip = '', $header = array()) {\r\n\treturn curl_post_with_ip($url, null, $ip, $header);\r\n}\r\n\r\n\/* http_post_with_ip,\u7528\u4e8eip\u89e3\u6790\u9519\u8bef\u6216\u8005\u9700\u8981\u6307\u5b9aip\u7684\u65f6\u5019\u4ee5post\u65b9\u5f0f\u83b7\u53d6\u6570\u636e\r\n * @param string $url \u8fdc\u7a0b\u5730\u5740\r\n * @param array|null $post_data \u4ee5post\u65b9\u5f0f\u63d0\u4ea4\u6570\u636e\uff0c\u5982\u679c\u6b64\u53c2\u6570\u4e3anull\uff0c\u5219\u4f7f\u7528get\u65b9\u5f0f\r\n * @param string $ip \u9700\u8981\u8bbf\u95ee\u7684ip\r\n * @param array $header \u9700\u8981\u6dfb\u52a0\u7684\u5176\u5b83header\u53c2\u6570\r\n * @return string|boolean \u8fd4\u56de\u83b7\u53d6\u7684\u5185\u5bb9\uff0c\u8bbf\u95ee\u5931\u8d25\u8fd4\u56defalse\r\n *\/\r\nfunction http_post_with_ip($url, $post_data = null, $ip = '', $header = array()) {\r\n\t\/* \u6700\u5927\u5faa\u73af\u8df3\u8f6c\u6b21\u6570 *\/\r\n\t$loop_max_count = 5;\r\n\r\n\tstatic $loop_count = 0;\r\n\t$errstr = '';\r\n\t$errno = '';\r\n\t\/* \u89e3\u6790URL *\/\r\n\t$url_array = parse_url($url);\r\n\tif ($url_array === false) {\r\n\t\t$loop_count = 0;\r\n\t\treturn false;\r\n\t}\r\n\t$host = $url_array['host'];\r\n\t\/* \u57df\u540d\u89e3\u6790\u5931\u8d25\u5219\u9000\u51fa *\/\r\n\tif (!$host) {\r\n\t\t$loop_count = 0;\r\n\t\treturn false;\r\n\t}\r\n\t\/* \u83b7\u53d6\u7aef\u53e3 *\/\r\n\t$port = isset($url_array['port']) ? $url_array['port'] : 80;\r\n\t\/* \u83b7\u53d6ip *\/\r\n\tif ($ip === '') {\r\n\t\t$ip = gethostbyname($host);\r\n\t}\r\n\r\n\t$fp = fsockopen($ip, $port, $errno, $errstr, 90);\r\n\tif (!$fp) {\r\n\t\t$loop_count = 0;\r\n\t\treturn false;\r\n\t} else {\r\n\t\t$out = (is_array($post_data) ? \"POST\" : \"GET\") . \" {$url} HTTP\/1.0\\r\\n\";\r\n\t\t\/* \u6700\u7ec8\u7684header\u6570\u7ec4 *\/\r\n\t\t$header_result = array();\r\n\t\t$header_result['Host'] = $host;\r\n\t\t\/* \u5904\u7406post\u6570\u636e *\/\r\n\t\tif (is_array($post_data)) {\r\n\t\t\t$post_values = array();\r\n\t\t\tforeach ($post_data as $key =&gt; $value) {\r\n\t\t\t\tarray_push($post_values, urlencode($key) . \"=\" . urlencode($value));\r\n\t\t\t}\r\n\t\t\t$post_content = implode(\"&amp;\", $post_values);\r\n\t\t\t$header_result['Content-type'] = \"application\/x-www-form-urlencoded\";\r\n\t\t\t$header_result['Content-length'] = strlen($post_content);\r\n\t\t}\r\n\t\t\/* \u8bbe\u7f6e\u9ed8\u8ba4\u7684header *\/\r\n\t\t$header_result['User-Agent'] = \"Mozilla\/5.0 (X11; Linux x86_64; rv:31.0) Gecko\/20100101 Firefox\/31.0\";\r\n\t\t$header_result = array_merge($header_result, $header);\r\n\t\t\/* \u8f7d\u5165\u7528\u6237\u7684header *\/\r\n\t\tforeach ($header_result as $key =&gt; $value) {\r\n\t\t\t$out .= $key . \": \" . $value . \"\\r\\n\";\r\n\t\t}\r\n\t\t$out .= \"Connection: close\\r\\n\\r\\n\";\r\n\t\tif (is_array($post_data)) {\r\n\t\t\t$out .= $post_content;\r\n\t\t}\r\n\t\tfputs($fp, $out);\r\n\r\n\t\t$response = '';\r\n\t\twhile (($line = fread($fp, 4096))) {\r\n\t\t\t$response .= $line;\r\n\t\t}\r\n\t\tfclose($fp);\r\n\r\n\t\t\/\/\u5206\u79bbHeader\u5934\u4fe1\u606f\r\n\t\t$pos = strpos($response, \"\\r\\n\\r\\n\");\r\n\t\t$header_content = substr($response, 0, $pos);\r\n\t\t$response = substr($response, $pos + 4);\r\n\r\n\t\tif (preg_match(\"\/HTTP\\\/\\d\\.\\d (\\d+) \/\", $header_content, $matches) !== 1) {\r\n\t\t\t$loop_count = 0;\r\n\t\t\treturn false;\r\n\t\t}\r\n\r\n\t\t$http_status = $matches[1];\r\n\t\t\/* \u5982\u679c\u662f30x\uff0c\u5c31\u8df3\u8f6c *\/\r\n\t\tif ($http_status == 301 || $http_status == 302) {\r\n\t\t\tif (preg_match(\"\/Location:(.*?)\\n\/\", $header_content, $matches) !== 1) {\r\n\t\t\t\t$loop_count = 0;\r\n\t\t\t\treturn false;\r\n\t\t\t}\r\n\t\t\t$url = trim($matches[1]);\r\n\t\t\t$loop_count++;\r\n\t\t\treturn curl_post_with_ip($url, $post_data, $ip, $header);\r\n\t\t}\r\n\r\n\t\t$loop_count = 0;\r\n\t\treturn $response;\r\n\t}\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u57df\u540d\u4e0eip\u4e0d\u76f8\u7b26\uff0c\u53c8\u65e0\u6cd5\u4fee\u6539\u7cfb\u7edfhosts\u65f6\uff0c\u6211\u4eec\u9700\u8981\u6307\u5b9aip\u6765\u83b7\u53d6\u8fdc\u7a0b\u6570\u636e\uff0c &#8230; <a class=\"more-link\" href=\"https:\/\/actom.me\/blog\/php%e4%bd%bf%e7%94%a8fsockopen%e6%8c%87%e5%ae%9aip%e9%80%9a%e8%bf%87get%e6%88%96%e8%80%85post%e8%8e%b7%e5%8f%96%e8%bf%9c%e7%a8%8b%e6%95%b0%e6%8d%ae.html\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/posts\/1111"}],"collection":[{"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/comments?post=1111"}],"version-history":[{"count":1,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/posts\/1111\/revisions"}],"predecessor-version":[{"id":1112,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/posts\/1111\/revisions\/1112"}],"wp:attachment":[{"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/media?parent=1111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/categories?post=1111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/tags?post=1111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}