如何用CURL并解释JSON

CURL *curl;
	CURLcode res;
	struct curl_slist *headers=NULL; // init to NULL is important
    headers = curl_slist_append(headers, "Accept: application/json");  

	curl = curl_easy_init();
	if(curl) {
		curl_easy_setopt(curl, CURLOPT_URL, "http://web.com/api/json/123");//cant get json file
		curl_easy_setopt(curl, CURLOPT_URL, "http://web.com/pages/123.html");//this returns entire webpage
		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
		curl_easy_setopt(curl, CURLOPT_RETURNTRANSFER, true);
	    res = curl_easy_perform(curl);

		if(CURLE_OK == res) {
			char *ct;
			// ask for the content-type
		    res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
			if((CURLE_OK == res) && ct)
		        printf("We received Content-Type: %s\n", ct);
		}
	}
    // always cleanup
    curl_easy_cleanup(curl);

//参考答案1

std::string ServerContent::DownloadJSO

N(std::string URL)
{
      
      CURL *curl;
      CURLcode res;
      struct curl_slist *headers=NULL; // init to NULL is important
      std::ostringstream oss;
       curl_slist_append(headers, "Accept: application/json");  
      curl_slist_append( headers, "Content-Type: application/json");
      curl_slist_append( headers, "charsets: utf-8");
       curl = curl_easy_init();

if(curl) {
            curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
            curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
            curl_easy_setopt(curl, CURLOPT_HTTPGET,1);
            curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
            curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,writer);
            res = curl_easy_perform(curl);
            if(CURLE_OK == res) {
                  char *ct;        
                  res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
                  if((CURLE_OK == res) && ct)
                        return *DownloadedResponse;
            }
      }

}

 
时间: 2025-01-04 15:32:05

如何用CURL并解释JSON的相关文章

curl终端请求json

curl -b 'uin=o123456789; [email protected]' -H "Content-Type: application/json" -X POST -d 'param={"key0":{"method":"add_money_for_qq_login","module":"pgg_bank_mt_svr","param":{"bi

curl javaSSm传入json数组的格式方法

curl与java结合传入get.post,head请求, 比如: curl的地址: curl -l 127.0.0.1:8080/spacobj/core/do?acid=100 -H "token:101hh" -H "Content-type: application/json" -X POST -d {"telNum":"13521389587","pass":"'my12345'&quo

js eval() 解释json数据

使用ajax调用服务端数据时,得到返回的数据格式为json时,如:{"error":{"code":-1,"msg":"文件格式有误,请上传图片(gif,jpg,jpeg,png,bmp)文件"}} 对于这样的json格式的数据,可以使用js的eval()格式进行转化为json解释. function upload(imgFile, image) { $.ajaxFileUpload({ url : '@{front.acco

php同curl post 发送json并返回json数据实例

<?php $arr = array( 'subject'=>'课程', 'loginName'=>'Durriya', 'password'=>'123' ); //json也可以 $data_string = json_encode($arr); //普通数组也行 //$data_string = $arr; echo $data_string; //echo '<br>'; //curl验证成功 $ch = curl_init("http://test.

curl命令发送JSON数据

curl -H "Content-Type: application/json" -X POST -d "{\"abc\":123}" "https://httpbin.org/post" windows,-d参数的数据,需要使用双引号,json里的双引号使用反斜杠转义才可以 -X:指定http请求的方法.如果使用了-d,默认是使用POST,可以省略-X参数. json数据放在一个文件里 curl -H "Conte

如何用Curl 来post xml 数据

因为登陆服务升级,密码策略变更,以前的测试脚本中的用户密码已经不能登陆,试图通过API直接更改密码,一种是直接update,一种是change,使用curl的时候均未成功. 最后索性重新用curl命令创建新的用户,一番摸索下来,注册用户仅支持私有端口.剩下的问题就是如何使用curl来postxml 文件. 经过一番查找,终于找到了curl使用post的命令: [java] view plain copy print? echo '<?xml version ->'|curl -X POST -

如何解释json的字符串

public void getToken(){ String json = getJedis().get("f2b9152f36424e8b8a454df9b50eb743"); JSONObject jsonobj = JSONObject.fromObject(json);// 将字符串转化成json对象 String name = jsonobj.getString("httpRefreshToken");// 获取字符串. String userId = j

如何用CURL将文件下载到本地指定文件夹

//获得跳转后的url地址 function getLocationUrl($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, TRUE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, "Fiddler"); curl_set

如何用简单易懂的例子解释隐马尔可夫模型?

隐马尔可夫(HMM)好讲,简单易懂不好讲. 本文回答简单易懂,https://www.zhihu.com/question/20962240/answer/33438846 原文地址:https://www.cnblogs.com/liuyihai/p/8449068.html