接口文档给的示例代码如下图:
<?php
$host = "http://id2meta.market.alicloudapi.com";
$path = "/id2meta";
$method = "GET";
$appcode = "你自己的AppCode";
$headers = array();
array_push($headers, "Authorization:APPCODE " . $appcode);
$querys = "identifyNum=identifyNum&identifyNumMd5=identifyNumMd5&userName=userName&userNameMd5=userNameMd5";
$bodys = "";
$url = $host . $path . "?" . $querys;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
if (1 == strpos("$".$host, "https://"))
{
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
var_dump(curl_exec($cu
解决PHP使用curl调用接口时的JSON解析与中文编码问题

文章讲述了在使用PHP的curl库调用接口时遇到的两个问题:返回值包含头部信息影响JSON解析,以及中文参数导致的非法参数错误。通过调整curl选项,设置Content-Type头,以及对中文参数使用URLencode处理,成功解决了这两个问题。接口参数的正确编码方式是百分号和16进制数字的组合,即URLencode编码。
最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



