{"id":156,"date":"2012-08-16T17:41:52","date_gmt":"2012-08-16T09:41:52","guid":{"rendered":"http:\/\/www.shuran.cn\/?p=156"},"modified":"2014-07-29T13:28:31","modified_gmt":"2014-07-29T05:28:31","slug":"php-%e6%a8%a1%e6%8b%9fpost-%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/www.shuran.cn\/?p=156","title":{"rendered":"PHP \u6a21\u62dfPOST \u6570\u636e"},"content":{"rendered":"<p>\n\t1.fsockopen\u65b9\u5f0f\uff0c\u53efpost\u56fe\u7247\uff1a <br \/>\n\/\/\u4f8b\u5b50\uff1asocket_post('http:\/\/site.com\/', array('key1'=&gt;'value1', 'key2'=&gt;'value2'), array('filekey'=&gt;'filepath'))<\/p>\n<pre class=\"prettyprint lang-js\">function socket_post($url, $postdata=array(), $files=array()) { \r\n\t$boundary = '-------'.substr(md5(rand(0,32000)),0,10); \r\n\t$encoded = \"\"; \r\n\tif($postdata){ \r\n\t\twhile(list($k,$v) = each($postdata)){ \r\n\t\t\t$encoded .= \"--$boundary\\r\\nContent-Disposition: form-data; \r\n\t\t\tname=\\\"\".rawurlencode($k).\"\\\"\\r\\n\\r\\n\"; \r\n\t\t\t$encoded .= rawurlencode($v).\"\\r\\n\"; \r\n\t\t} \r\n\t} \r\n\tif($files){ \r\n\t\tforeach($files as $name=&gt;$file){ \r\n\t\t\t$ext= strtolower(strrchr($file,\".\")); \r\n\t\t\tswitch($ext){ \r\n\t\t\t\tcase '.gif': $type = \"image\/gif\"; break; \r\n\t\t\t\tcase '.jpg': $type = \"image\/jpeg\"; break; \r\n\t\t\t\tcase '.png': $type = \"image\/png\"; \r\n\t\t\t\tbreak; default: $type = \"image\/jpeg\"; \r\n\t\t\t} \r\n\t\t\t$encoded .= \"--$boundary\\r\\nContent-Disposition: form-data; \r\n\t\t\tname=\\\"\".rawurlencode($name).\"\\\"; \r\n\t\t\tfilename=\\\"$file\\\"\\r\\nContent-Type: $type\\r\\n\\r\\n\"; \r\n\t\t\t$encoded .= join(\"\", file($file)); $encoded .= \"\\r\\n\"; \r\n\t\t} \r\n\t} \r\n\t$encoded .= \"--$boundary--\\r\\n\"; \r\n\t$length = strlen($encoded); \r\n\t$uri = parse_url($url); \r\n\t$fp = fsockopen($uri['host'], $uri['port'] ? $uri['port'] : 80); \r\n\tif(!$fp) return \"Failed to open socket to \".$uri['host']; \r\n\tfputs($fp, sprintf(\"POST %s%s%s HTTP\/1.0\\r\\n\", $uri['path'], $uri['query'] ? \"?\" : \"\", $uri['query'])); \r\n\tfputs($fp, \"Host: \".$uri['host'].\"\\r\\n\"); \r\n\tfputs($fp, \"Content-type: multipart\/form-data; boundary=$boundary\\r\\n\"); \r\n\tfputs($fp, \"Content-length: $length\\r\\n\"); \r\n\tfputs($fp, \"Connection: close\\r\\n\\r\\n\"); \r\n\tfputs($fp, $encoded); \r\n\t$results = \"\"; \r\n\twhile(!feof($fp)){ \r\n\t\t$results .= fgets($fp,1024); \r\n\t\t} \r\n\tfclose($fp); \r\n\treturn preg_replace('\/^HTTP(.*?)\\r\\n\\r\\n\/s','',$results); \r\n}<\/pre>\n<p>2.file_get_contents\u65b9\u5f0f\uff0c\u4e0d\u80fdpost\u6587\u4ef6\uff1a&nbsp;<\/p>\n<pre class=\"prettyprint lang-js\">\tfunction Post($url, $post = null) { \r\n\t\t$context = array(); \r\n\t\tif (is_array($post)) { \r\n\t\t\tksort($post); \r\n\t\t\t$context['http'] = array ( \r\n\t\t\t\t'method' =&gt; 'POST', \r\n\t\t\t\t'header'=&gt;'Content-type: application\/x-www-form-urlencoded', \r\n\t\t\t\t'content' =&gt; http_build_query($post, '', '&amp;')\r\n\t\t\t\t); \r\n\t\t} \r\n\t\treturn file_get_contents($url, false, stream_context_create($context)); \r\n\t}<\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.fsockopen\u65b9\u5f0f\uff0c\u53efpost\u56fe\u7247\uff1a \/\/\u4f8b\u5b50\uff1asocket_post('http:\/\/site.co\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.shuran.cn\/?p=156\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-156","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/posts\/156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=156"}],"version-history":[{"count":3,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/posts\/156\/revisions"}],"predecessor-version":[{"id":525,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/posts\/156\/revisions\/525"}],"wp:attachment":[{"href":"https:\/\/www.shuran.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}