{"id":156,"date":"2015-06-25T09:41:38","date_gmt":"2015-06-25T01:41:38","guid":{"rendered":"https:\/\/www.webteach.tw\/?p=156"},"modified":"2015-06-25T09:41:38","modified_gmt":"2015-06-25T01:41:38","slug":"curl","status":"publish","type":"post","link":"https:\/\/www.webteach.tw\/?p=156","title":{"rendered":"[ PHP ] &#8211; CURL POST DEMO FUNCTION \u7c21\u6613\u578bcurl \u50b3post\u53c3\u6578\u7bc4\u4f8b"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">CURL POST DEMO FUNCTION \u7c21\u6613\u578bcurl \u50b3post\u53c3\u6578\u7bc4\u4f8b<\/p>\n\n\n<p class=\"wp-block-paragraph\">\u820a\u7248\u7bc4\u4f8b\uff1a<\/p>\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">$postUrl=$url;\/\/\u7db2\u5740\n$postBODY = array('name' => 'jeff','age'=>18,'sex'=>1);\/\/post\u53c3\u6578\n$getBody = my_curl($postUrl,$postBODY);\nfunction my_curl($url,$post){\n    $ch = curl_init();\n    curl_setopt($ch, CURLOPT_URL, $url);\n    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,  'POST');\n    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);\n    $result = curl_exec($ch);\n    curl_close ($ch);\n    return $result;\n}<\/pre>\n\n\n<p class=\"wp-block-paragraph\">\u65b0\u7248\u7bc4\u4f8b \u5c07 opt \u53c3\u6578\u9663\u5217\u5316<\/p>\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">function curl_post($url, $post_data) {\n\t$url = $url;\n\t$data = $post_data;\n\t$setopts = array(\n\t\tCURLOPT_URL\t\t=> $url,\n\t\tCURLOPT_RETURNTRANSFER\t=> true,\n\t\tCURLOPT_HTTPHEADER\t=> array(\n\t\t\t\t\t\t\"Content-type: multipart\/form-data\",\n\t\t\t\t\t\t'Content-Length: '.strlen($data),\n\t\t\t\t\t\t\t\t\t),\n\t\tCURLOPT_POST\t\t\t=> true,\n\t\tCURLOPT_POSTFIELDS\t\t=> $data,\n\t\tCURLOPT_CUSTOMREQUEST\t=> \"POST\"\n\t);\n\t$curl = curl_init();\n\tcurl_setopt_array($curl, $setopts);\n\t$response = curl_exec($curl);\n\tcurl_close($curl);\n\treturn $response;\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>CURL POST DEMO FUNCTION \u7c21\u6613\u578bcurl \u50b3post\u53c3\u6578\u7bc4\u4f8b<\/p>\n","protected":false},"author":1,"featured_media":2201,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[191,448,467],"class_list":["post-156","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-curl","tag-php","tag-post","hvn-theme-has-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/posts\/156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=156"}],"version-history":[{"count":0,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/posts\/156\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/media\/2201"}],"wp:attachment":[{"href":"https:\/\/www.webteach.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}