如何从curl_exec解析结果以提取PHP中的json数据

这篇博客讨论了如何在PHP中处理curl_exec返回的混合数据,特别是当数据包含JSON时。作者遇到的问题是尝试从HTTP响应中解析JSON,而该响应还包含了HTTP头信息。解决方案是使用curl_setopt()设置CURLOPT_HEADER为false来忽略HTTP头,或者通过分割字符串来分离头和主体。然后,可以使用json_decode()函数解析JSON数据,并编辑其中的值。例如,更改'error'字段的值。

我收到了包含json数据和其他数据的执行curl_exec的结果。我无法弄清楚如何编辑这个结果。特别是,我需要从结果中包含的json数据编辑一个值。例如,给出以下结果:如何从curl_exec解析结果以提取PHP中的json数据

RESPONSE: HTTP/1.1 400 Bad Request 
Server: nginx 
Date: Sat, 10 Jan 2015 17:31:02 GMT 
Content-Type: application/json 
Content-Length: 25 
Connection: keep-alive 
Keep-Alive: timeout=10 

{"error":"invalid_grant"} 

如何更改“错误”的值?只是使用json_decode本身似乎并不是一个有效的方法。它返回NULL结果:

$obj = json_decode($response); 

建议?

来源

2015-01-10 JeffB6688

A

回答

3

你试过:

curl_setopt($s,CURLOPT_HEADER,false); 

基本上,你正在接收来自服务器的完整响应:

# these are the headers 
RESPONSE: HTTP/1.1 400 Bad Request 
Server: nginx 
Date: Sat, 10 Jan 2015 17:31:02 GMT 
Content-Type: application/json 
Content-Length: 25 
Connection: keep-alive 
Keep-Alive: timeout=10 

# This is the body. 
{"error":"invalid_grant"} 

通过告诉卷曲忽略标题,你应该只得到{"error":"invalid_grant"}

现在,所有这些说,标题分隔两个换行符的身体。所以你应该也可以这样解析:

$val = curl_exec(); 

// list($header,$body) = explode("\n\n", $val); won't work: \n\n is a valid value for 
// body, so we only care about the first instance. 
$header = substr($val, 0, strpos($val, "\n\n")); 
$body = substr($val, strpos($val, "\n\n") + 2); 
// You *could* use list($header,$body) = preg_split("#\n\n#", $val, 2); because that 
// will create an array of two elements. 

// To get the value of *error*, you then 
$msg = json_decode($body); 
$error = $msg->error; 

/* 
The following are because you asked how to "change the value of `error`". 
You can safely ignore if you don't want to put it back together. 
*/ 
// To set the value of the error: 
$msg->error = 'Too many cats!'; 

// to put everything back together: 
$replaced = $header . "\n\n" . json_encode($msg); 
<?php /** * Created by PhpStorm. * User: FZS * Time: 2020/10/10 17:50 */ //---------------------------------- // 皮肤分析 调用类 //---------------------------------- class freeApi { private $apiKey = false; private $apiSecret = false; private $apiUrl = 'https://api-cn.faceplusplus.com/facepp/v1/skinanalyze'; public function __construct($apikey,$apiSecret){ $this->apiKey = $apikey; $this->apiSecret = $apiSecret; } /** * 将JSON内容转为数据,并返回 * @param string $content [内容] * @return array */ public function returnArray($content){ return json_decode($content,true); } /** * 获取结果 * @return array */ public function getResult(){ $params = [ "api_key" => $this->apiKey, "api_secret" => $this->apiSecret, "image_url" => "", ]; $params = http_build_query($params); return $this->returnArray($this->freeApiCurl($this->apiUrl,$params,1)); } /** * 请求接口返回内容 * @param string $url [请求的URL地址] * @param string $params [请求的参数] * @param int $ipost [是否采用POST形式] * @return string */ public function freeApiCurl($url,$params=false,$ispost=0){ $ch = curl_init(); curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); curl_setopt( $ch , CURLOPT_SSL_VERIFYHOST , false ); curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER , false ); curl_setopt( $ch, CURLOPT_USERAGENT , 'free-api' ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 ); curl_setopt( $ch, CURLOPT_TIMEOUT , 60); curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true ); if( $ispost ) { curl_setopt( $ch , CURLOPT_POST , true ); curl_setopt( $ch , CURLOPT_POSTFIELDS , $params ); curl_setopt( $ch , CURLOPT_URL , $url ); } else { if($params){ curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params ); }else{ curl_setopt( $ch , CURLOPT_URL , $url); } } $response = curl_exec( $ch ); if ($response === FALSE) { return false; } curl_close( $ch ); return $response; } } 解释一下代码
06-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值