curl发送json请求(curl 返回json)

本文目录一览:

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

一般中文json_encode之后会变成\uxxxx的格式了,只要使用正规的json_encode处理,

不需要考虑中文问题。

至于如何post数据到服务器,需要设定header,参考代码如下:

?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;

    

服务端需要使用$data = file_get_contents(‘php://input’);获取数据。

更多PHP cURL内容请参考我的博客《PHP cURL实现模拟登录与采集使用方法详解教程》

php通过curl发送post json给https产生502错误问题!

curl_setopt(self::$ch, CURLOPT_USERAGENT, “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/600.3.18 (KHTML, like Gecko) Version/8.0.3 Safari/600.3.18”);

加一个这个参数模拟浏览器,看看有没有作用

有些服务器非浏览器访问就会502

为什么要使用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:%s\n”, 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;

}

PHP如何通过Post请求发送Json数据

首先要把数据转换成json格式,再通过curl方法调用接口并传参数

代码如下:

$keyword = urlencode($_POST[‘keyword’]);

$parameters = json_encode(array(‘keyWord’=$keyword,’areaCode’=’*’));

$post_data[‘appToken’] = “323ds7674354fds32fdsda60173”;//随便写的

$post_data[‘parameters’] = $parameters;

$url = ”;//随便写的

$ch = curl_init();

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//用post方法传送参数

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch);

curl_close($ch);

之后就返回数据即可。

原创文章,作者:TNL5J,如若转载,请注明出处:https://www.506064.com/n/128395.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
TNL5J的头像TNL5J
上一篇 2024-10-03 23:25
下一篇 2024-10-03 23:25

相关推荐

发表回复

登录后才能评论