{"id":173,"date":"2023-02-19T19:06:55","date_gmt":"2023-02-19T11:06:55","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=173"},"modified":"2023-02-19T19:07:13","modified_gmt":"2023-02-19T11:07:13","slug":"php-curl-post-json","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/19\/php-curl-post-json\/","title":{"rendered":"PHP\u4f7f\u7528curl\u63d0\u4ea4json\u683c\u5f0f\u6570\u636e"},"content":{"rendered":"<pre><code class=\"language-php\">$params = array();\nforeach ($_POST as $key =&gt; $value) {\n    $params[$key] = $value;\n}\n\necho request_post($url, $params);<\/code><\/pre>\n<p><!-- more --><\/p>\n<pre><code class=\"language-php\">function request_post($url = &#039;&#039;, $params = &#039;&#039;, $timeout = 5000)\n{\n    if (empty($url) || empty($params)) {\n        return &#039;{}&#039;;\n    }\n\n    $header = array(\n        &quot;Accept: application\/json&quot;,\n        &quot;Content-Type: application\/json&quot;,\n    );\n\n    $jsonData = json_encode($params);\n    $postUrl = $url;\n    $ch = curl_init(); \/\/\u521d\u59cb\u5316curl\n    curl_setopt($ch, CURLOPT_URL, $postUrl);\n    \/\/curl_setopt($ch, CURLOPT_CUSTOMREQUEST, &quot;POST&quot;);\n    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n    curl_setopt($ch, CURLOPT_POST, true);\n    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);\n    curl_setopt ( $curl, CURLOPT_TIMEOUT, $timeout);\n    curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, false);\n    curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false);\n    $data = curl_exec($ch);\/\/\u8fd0\u884ccurl\n    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n    curl_close($ch);\n    $response = array();\n\n    $response[&#039;httpCode&#039;] = $httpCode;\n    $response[&#039;data&#039;] = json_decode($data);\n\n    return json_encode($response);\n}\n\nfunction request_get($url = &#039;&#039;, $params = &#039;&#039;, $timeout = 5000)\n{\n    if (empty($url) || empty($params)) {\n        return &#039;{}&#039;;\n    }\n\n    $header = array(\n        &quot;Accept: application\/json&quot;,\n        \/\/&quot;Content-Type: application\/json&quot;,\n    );\n\n    $linkedData = buildLinkedParams($params);\n    $getUrl = $url . &#039;?&#039; . $linkedData;\n\n    $ch = curl_init();\/\/\u521d\u59cb\u5316curl\n    curl_setopt($ch, CURLOPT_URL, $getUrl);\n    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n    curl_setopt ( $curl, CURLOPT_TIMEOUT, $timeout);\n    curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, false);\n    curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false);\n    $data = curl_exec($ch);\/\/\u8fd0\u884ccurl\n    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n    curl_close($ch);\n    $response = array();\n\n    $response[&#039;httpCode&#039;] = $httpCode;\n    $response[&#039;data&#039;] = json_decode($data);\n\n    return json_encode($response);\n}\n\n\/**\n * \u62fc\u63a5\u53c2\u6570\uff1akey1=value1&amp;key2=value2\n * @param $params\n * return\n *\/\nfunction buildLinkedParams($params)\n{\n    $str = &quot;&quot;;\n    while (list ($key, $val) = each ($params)) {\n        $str .= $key . &quot;=&quot; . $val . &quot;&amp;&quot;;\n    }\n    \/\/\u53bb\u9664\u6700\u540e&amp;\u7b26\n    $str = substr($str, 0, count($str)-2);\n    \/\/\u53bb\u9664\u8f6c\u4e49\u7b26\n    if (get_magic_quotes_gpc()) {\n        $str = stripslashes($str);\n    }\n    return $str;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>$params = array(); foreach ($_POST as $key =&gt; $value [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69],"tags":[73,74],"class_list":["post-173","post","type-post","status-publish","format-standard","hentry","category-php-basic","tag-curl","tag-json"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=173"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/173\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}