{"id":975,"date":"2013-06-03T12:05:56","date_gmt":"2013-06-03T04:05:56","guid":{"rendered":"http:\/\/www.poloo.org\/?p=975"},"modified":"2013-06-03T12:09:35","modified_gmt":"2013-06-03T04:09:35","slug":"%e5%9c%a8php%e4%b8%ad%e8%b0%83%e7%94%a8api%e6%8e%a5%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=975","title":{"rendered":"\u5728PHP\u4e2d\u8c03\u7528API\u63a5\u53e3|Post\u6570\u636e\u7684\u65b9\u6cd5"},"content":{"rendered":"<h3>PHP\u4e2d\u8c03\u7528\u63a5\u53e3<\/h3>\n<p>\u5982\uff1ahttp:\/\/localhost\/operate.php?act=get_user_list&amp;type=json<\/p>\n<p>\u5728\u8fd9\u91ccoperate.php\u76f8\u5f53\u4e8e\u4e00\u4e2a\u63a5\u53e3\uff0c\u5176\u4e2dget_user_list \u662f\u4e00\u4e2aAPI\uff08\u83b7\u53d6\u7528\u6237\u5217\u8868\uff09\uff0c\u8bb2\u6c42\u8fd4\u56de\u7684\u6570\u636e\u7c7b\u578b\u4e3aJSON\u683c\u5f0f\u3002<\/p>\n<p>\u4f60\u53ea\u9700\u8981\u5728\u4f60PHP\u4ee3\u7801\u4e2d\u6267\u884c\u8fd9\u6761\u94fe\u63a5\u4ed6\u5c31\u4f1a\u8fd4\u56de\u3002<br \/>\nGET\u65b9\u5f0f\u7684\u76f4\u63a5\u4f7f\u7528<br \/>\n$file_contents = file_get_content(&#8216;http:\/\/localhost\/operate.php?act=get_user_list&amp;type=json&#8217;)<\/p>\n<p>POST\u65b9\u5f0f\u5f97\u7528\u4e0b\u9762\u7684(\u9700\u8981\u5f00\u542fPHP curl\u652f\u6301)\u3002<\/p>\n<p><!--more--><\/p>\n<pre class=\"brush:php\">$url = 'http:\/\/localhost\/operate.php?act=get_user_list&amp;type=json';\r\n$ch = curl_init ();\r\ncurl_setopt ( $ch, CURLOPT_URL, $url );\r\ncurl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );\r\ncurl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 10 );\r\ncurl_setopt ( $ch, CURLOPT_POST, 1 ); \/\/\u542f\u7528POST\u63d0\u4ea4\r\n$file_contents = curl_exec ( $ch );\r\ncurl_close ( $ch );<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3>post\u6570\u636e\u7684\u4e09\u79cd\u65b9\u6cd5<\/h3>\n<p>&nbsp;<\/p>\n<pre class=\"brush:php\">&lt;?php \r\n\/\/ PHP POST\u6570\u636e\u7684\u4e09\u79cd\u65b9\u6cd5\r\n\/\/ php\u6709\u4e09\u79cd\u65b9\u6cd5\u53ef\u4ee5post\u6570\u636e,\u5206\u522b\u4e3aCurl\u3001socket\u3001file_get_contents:\r\n\r\n\/**\r\n * Socket\u7248\u672c\r\n * \u4f7f\u7528\u65b9\u6cd5\uff1a\r\n * $post_string = \"app=socket&amp;version=beta\";\r\n * request_by_socket('facebook.cn','\/restServer.php',$post_string);\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    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: \" . (strlen($post_string) + 8) . '\\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    while ($str = trim(fgets($socket, 4096))) {\r\n        $header .= $str;\r\n    } \r\n    $data = \"\";\r\n    while (!feof($socket)) {\r\n        $data .= fgets($socket, 4096);\r\n    } \r\n    return $data;\r\n} \r\n\r\n\/**\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:\/\/facebook.cn\/restServer.php',$post_string);\r\n *\/\r\nfunction request_by_curl($remote_server, $post_string)\r\n{\r\n    $ch = curl_init();\r\n    curl_setopt($ch, CURLOPT_URL, $remote_server);\r\n    curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);\r\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\r\n    curl_setopt($ch, CURLOPT_USERAGENT, \"Jimmy's CURL Example beta\");\r\n    $data = curl_exec($ch);\r\n    curl_close($ch);\r\n    return $data;\r\n} \r\n\r\n\/**\r\n * \u5176\u5b83\u7248\u672c\r\n * \u4f7f\u7528\u65b9\u6cd5\uff1a\r\n * $post_string = \"app=request&amp;version=beta\";\r\n * request_by_other('http:\/\/facebook.cn\/restServer.php',$post_string);\r\n *\/\r\nfunction request_by_other($remote_server, $post_string)\r\n{\r\n    $context = array(\r\n        'http' =&gt; array(\r\n            'method' =&gt; 'POST',\r\n            'header' =&gt; 'Content-type: application\/x-www-form-urlencoded' .\r\n                        '\\r\\n'.'User-Agent : Jimmy\\'s POST Example beta' .\r\n                        '\\r\\n'.'Content-length:' . strlen($post_string) + 8,\r\n            'content' =&gt; 'mypost=' . $post_string)\r\n        );\r\n    $stream_context = stream_context_create($context);\r\n    $data = file_get_contents($remote_server, false, $stream_context);\r\n    return $data;\r\n} \r\n\r\n?&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u4e2d\u8c03\u7528\u63a5\u53e3 \u5982\uff1ahttp:\/\/localhost\/operat&#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":[5,18],"tags":[128,36,159,94],"class_list":["post-975","post","type-post","status-publish","format-standard","hentry","category-Note","category-Php","tag-api","tag-php-2","tag-post","tag-94"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/975","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=975"}],"version-history":[{"count":3,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/975\/revisions"}],"predecessor-version":[{"id":977,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/975\/revisions\/977"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}