首页 > 编程知识 正文

curl送json文件(curl 发送json)

时间:2023-12-24 12:05:53 阅读:321201 作者:OOCN

本文目录一览:

在php 的curl函数来GET一个地址,得到的响应是一个json文件,怎么来操作这个文件

打开看了一下, 后缀是json, 但里面的代码是JavaScript代码!

?php

$Json =  file_get_contents(';class=logintpl=mntangram=true');

preg_match_all('/bdPass.api.params.login_token='([^']*)';/is', $Json, $Ken);

$ToKen = $Ken[1][0];

echo $ToKen;

如何用curl post 一段包含中文json的文本到服务器

我的博客《PHP cURL实现模拟登录与采集使用方法详解》第十一点发送与获取json数据对此类问题做了详细的讲解,下面是代码示例:

?php

    #json数据

    $url = '';

    $data = '{"a":"b"}';

    $length = strlen($data);

    $header = array(

        'Content-Length: ' . $length,   //不是必需的

        'Content-Type: text/json',

    );

    $ch                 = curl_init($url);  //初始化curl

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

    curl_setopt($ch, CURLOPT_POST, 1);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    $content = curl_exec($ch);  //执行并存储结果

    curl_close($ch);

    echo $content;

    #n分割数据

    $data = [

        'name:Zjmainstay',

        'website:',

    ];

    $data = implode("n", $data);

    #分割数据

    $data = 'name:Zjmainstaywebsite:';

更多详情,包括服务端如何接收此类数据,请查看博客:

php用curl的post方法传递json包的时候,接受方是怎么获取的呢

假设POST的数据为:{"data":"abc"}

POST参数为:data

同样以PHP为例,接受并处理请求的相关代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

?php

extract($_POST); // 将数组中的key摊成变量,并导入key对应的值

if (!empty($data))

{

$data = json_decode($data); // json 字符串解码成 json 数据

var_dump($data); // 打印 json 数据

// 输出结果

object(stdClass)[1]

public 'data' = string 'abc' (length=3)

}

为什么要使用curl传输json

//使用curl库,以post方式向服务器发送json数据

//json数据的组合可以参考jsoncpp库,也可以按json格式自己组合字符串

//注意事项,以下代码不可以多线程执行,如果多线程执行,需要加锁进行控制,否则会运行崩溃

[cpp] view plain copy

#include curl/curl.h

#include string

#include exception

int main(int argc, char *argv[])

{

char szJsonData[1024];

memset(szJsonData, 0, sizeof(szJsonData));

std::string strJson = "{";

strJson += ""user_name" : "test",";

strJson += ""password" : "test123"";

strJson += "}";

strcpy(szJsonData, strJson.c_str());

try

{

CURL *pCurl = NULL;

CURLcode res;

// In windows, this will init the winsock stuff

curl_global_init(CURL_GLOBAL_ALL);

// get a curl handle

pCurl = curl_easy_init();

if (NULL != pCurl)

{

// 设置超时时间为1秒

curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 1);

// First set the URL that is about to receive our POST.

// This URL can just as well be a

// https:// URL if that is what should receive the data.

curl_easy_setopt(pCurl, CURLOPT_URL, "");

//curl_easy_setopt(pCurl, CURLOPT_URL, "");

// 设置http发送的内容类型为JSON

curl_slist *plist = curl_slist_append(NULL,

"Content-Type:application/json;charset=UTF-8");

curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, plist);

// 设置要POST的JSON数据

curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, szJsonData);

// Perform the request, res will get the return code

res = curl_easy_perform(pCurl);

// Check for errors

if (res != CURLE_OK)

{

printf("curl_easy_perform() failed:%sn", curl_easy_strerror(res));

}

// always cleanup

curl_easy_cleanup(pCurl);

}

curl_global_cleanup();

}

catch (std::exception ex)

{

printf("curl exception %s.n", ex.what());

}

return 0;

}

如何使用cURL来获取jSON数据和解码数据

你将$hello直接解析成了jsons 如果服务器端要接收。 你可以在

$hello=json_en......;下一行加上

$data=array('hello'=$hello);

将curl_setopt($ch,CURLOPT_POSTFIELDS,$hello);改成curl_setopt($ch,CURLOPT_POSTFIELDS,$data);

在URL端你可以用var_dump($_POST);

windows下使用curl利用post发送json数据时注意事项

在window中linux格式下的单引号要改成双引号,json格式数据中双引号要加转义

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。