search
Homephp教程php手册php 通过curl post发送json数据实例

利用php curl发送json数据与curl post其它数据是一样的,下面我来给大家总结几个关于curl post发送json数据实例,希望能加深各位对curl post json数据的理解吧。

例1代码如下:

<?php
$data = array(
    "name" => "Hagrid",
    "age" => "36"
);
$data_string = json_encode($data);
$ch = curl_init(&#39;http://api.local/rest/users&#39;);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    &#39;Content-Type: application/json&#39;,
    &#39;Content-Length: &#39; . strlen($data_string)
));
$result = curl_exec($ch);
?>

例2,代码如下:

<?php
function http_post_data($url, $data_string) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        &#39;Content-Type: application/json; charset=utf-8&#39;,
        &#39;Content-Length: &#39; . strlen($data_string)
    ));
    ob_start();
    curl_exec($ch);
    $return_content = ob_get_contents();
    ob_end_clean();
    $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    return array(
        $return_code,
        $return_content
    );
}
$url = "http://xx.xx.cn";
$data = json_encode(array(
    &#39;a&#39; => 1,
    &#39;b&#39; => 2
));
list($return_code, $return_content) = http_post_data($url, $data);
?>

例3代码如下:

<?php
$data = &#39; { 
     "button":[ 
     {     
          "type":"click", 
          "name":"今日歌曲", 
          "key":"V1001_TODAY_MUSIC" 
      }, 
      { 
           "type":"click", 
           "name":"歌手简介", 
           "key":"V1001_TODAY_SINGER" 
      }, 
      { 
           "name":"菜单", 
           "sub_button":[ 
            { 
               "type":"click", 
               "name":"hello word", 
               "key":"V1001_HELLO_WORLD" 
            }, 
            { 
               "type":"click", 
               "name":"赞一下我们", 
               "key":"V1001_GOOD" 
            }] 
       }] 
 }&#39;;
$ch = curl_init($urlcon); //请求的URL地址
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //$data JSON类型字符串
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    &#39;Content-Type: application/json&#39;,
    &#39;Content-Length: &#39; . strlen($data)
));
$data = curl_exec($ch);
print_r($data); //创建成功返回:{"errcode":0,"errmsg":"ok"}
?>

小结,我们发现最核心的一句代码就是Content-Type: application/json;这个是文件格式类型了.


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment