{"id":143,"date":"2018-09-06T17:05:40","date_gmt":"2018-09-06T09:05:40","guid":{"rendered":"https:\/\/wp.dj47.top\/?p=143"},"modified":"2021-10-20T16:19:03","modified_gmt":"2021-10-20T08:19:03","slug":"php-%e5%8f%91%e9%80%81post%e8%af%b7%e6%b1%82","status":"publish","type":"post","link":"https:\/\/www.djlog.cn\/?p=143","title":{"rendered":"PHP \u53d1\u9001POST\u8bf7\u6c42"},"content":{"rendered":"<pre class=\"language-php\"><code>\/** \r\n\r\n * \u53d1\u9001post\u8bf7\u6c42 \r\n\r\n * @param string $url \u8bf7\u6c42\u5730\u5740 \r\n\r\n * @param array $post_data post\u952e\u503c\u5bf9\u6570\u636e \r\n\r\n * @return string \r\n\r\n *\/  \r\n\r\nfunction send_post($url, $post_data) {  \r\n\r\n  \r\n\r\n  $postdata = http_build_query($post_data);  \r\n\r\n  $options = array(  \r\n\r\n    'http' =&gt; array(  \r\n\r\n      'method' =&gt; 'POST',  \r\n\r\n      'header' =&gt; 'Content-type:application\/x-www-form-urlencoded',  \r\n\r\n      'content' =&gt; $postdata,  \r\n\r\n      'timeout' =&gt; 15 * 60 \/\/ \u8d85\u65f6\u65f6\u95f4\uff08\u5355\u4f4d:s\uff09  \r\n\r\n    )  \r\n\r\n  );  \r\n\r\n  $context = stream_context_create($options);  \r\n\r\n  $result = file_get_contents($url, false, $context);  \r\n\r\n  \r\n\r\n  return $result;  \r\n\r\n}  \r\n\r\n  \r\n\r\n\/\/\u4f7f\u7528\u65b9\u6cd5  \r\n\r\n$post_data = array(  \r\n\r\n  'username' =&gt; 'stclair2201',  \r\n\r\n  'password' =&gt; 'handan'  \r\n\r\n);  \r\n\r\nsend_post('http:\/\/www.qianyunlai.com', $post_data);  \r\n\r\n  \r\n\r\n  \r\n\r\n  \r\n\r\n  \r\n\r\n&lt;?php  \r\n\r\n\/** \r\n\r\n * Socket\u7248\u672c \r\n\r\n * \u4f7f\u7528\u65b9\u6cd5\uff1a \r\n\r\n * $post_string = \"app=socket&amp;version=beta\"; \r\n\r\n * request_by_socket('chajia8.com', '\/restServer.php', $post_string); \r\n\r\n *\/  \r\n\r\nfunction request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30) {  \r\n\r\n  $socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);  \r\n\r\n  if (!$socket) die(\"$errstr($errno)\");  \r\n\r\n  fwrite($socket, \"POST $remote_path HTTP\/1.0\");  \r\n\r\n  fwrite($socket, \"User-Agent: Socket Example\");  \r\n\r\n  fwrite($socket, \"HOST: $remote_server\");  \r\n\r\n  fwrite($socket, \"Content-type: application\/x-www-form-urlencoded\");  \r\n\r\n  fwrite($socket, \"Content-length: \" . \uff08strlen($post_string) + 8\uff09 . \"\");  \r\n\r\n  fwrite($socket, \"Accept:*\/*\");  \r\n\r\n  fwrite($socket, \"\");  \r\n\r\n  fwrite($socket, \"mypost=$post_string\");  \r\n\r\n  fwrite($socket, \"\");  \r\n\r\n  $header = \"\";  \r\n\r\n  while ($str = trim(fgets($socket, 4096))) {  \r\n\r\n    $header .= $str;  \r\n\r\n  }  \r\n\r\n  \r\n\r\n  $data = \"\";  \r\n\r\n  while (!feof($socket)) {  \r\n\r\n    $data .= fgets($socket, 4096);  \r\n\r\n  }  \r\n\r\n  \r\n\r\n  return $data;  \r\n\r\n}  \r\n\r\n?&gt;  \r\n\r\n  \r\n\r\n&lt;?php  \r\n\r\n\/**  \r\n\r\n * Curl\u7248\u672c  \r\n\r\n * \u4f7f\u7528\u65b9\u6cd5\uff1a  \r\n\r\n * $post_string = \"app=request&amp;version=beta\";  \r\n\r\n * request_by_curl('http:\/\/www.qianyunlai.com\/restServer.php', $post_string);  \r\n\r\n *\/  \r\n\r\nfunction request_by_curl($remote_server, $post_string) {  \r\n\r\n  $ch = curl_init();  \r\n\r\n  curl_setopt($ch, CURLOPT_URL, $remote_server);  \r\n\r\n  curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);  \r\n\r\n  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  \r\n\r\n  curl_setopt($ch, CURLOPT_USERAGENT, \"qianyunlai.com's CURL Example beta\");  \r\n\r\n  $data = curl_exec($ch);  \r\n\r\n  curl_close($ch);  \r\n\r\n  \r\n\r\n  return $data;  \r\n\r\n}  \r\n\r\n?&gt;  <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/** * \u53d1\u9001post\u8bf7\u6c42 * @param string $url \u8bf7\u6c42\u5730\u5740 * @param array [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"class_list":["post-143","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts\/143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=143"}],"version-history":[{"count":0,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts\/143\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}