HTTP Rest API向我显示上面的值:
id 514
filial 5
name "COD. 514 20 Mb RES. TRL"
nome_amigavel "20 Mb"
mensalidade "89.90"
desconto "0.00"
ativo 1
tipo 1
instalacao "300.00"
bnd_up 2000
bnd_down 20000
1
id 422
filial 4
name "COD. 069 30 Mb TRANSPORTE"
nome_amigavel "30 Mb"
mensalidade "1500.00"
desconto "0.00"
ativo 1
tipo 3
instalacao "1500.00"
bnd_up 0
bnd_down 30000
2
我如何"打印"或者" Echo" PHP文件中的特定值或单个值????
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://services.west.net.br/rest/server.php/planos",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Basic YXaBepOmsadsahpaacGVwwaybassdwsadsadsawd3BpYSBw0cmddF2YWdaalbSBiaXBdlbmFkbw==",
"cache-control: no-cache",
"content-type: application/json",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
我试试这个,但没有成功,我在休息api的新手,并尝试从网上的一些例子,请帮助一些!
答案 0 :(得分:0)
使用以下代码
$curl_response = curl_exec($curl); //<!---- $curl_responce instead of $output
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' .
var_export($info)); //<!--- this is pointless IMO
}
curl_close($curl);
$decoded = json_decode($curl_response); ///<!--- set to results to $decoded, also no second value ie. "json_decode($curl_response, 1);"
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
var_export($decoded->response);
//==================================//
// looks like someone just dropped the code below
// this line in. ha ha.
$result = json_decode($output, 1); //<!--- output does not exist, but this is already done above. so use $decoded instead of $output.
// check if an id came back
if (!empty($result['id'])) {
$deal_id = $result['id'];
return $deal_id;
} else {
return false;
}
echo $deal_id;
永远不会设置变量$output
,而是使用$decoded
。这是因为$result
也不存在,而是在上面的代码中$curl_response
。感觉你已经解码了,不需要再解码了。
那就是说,在json_decode
那里,第二个参数没有设置为true
,在这种情况下,你得到一个对象而不是你想象的数组。
http://php.net/manual/en/function.json-decode.php
混合json_decode(字符串$ json [,bool $ assoc = false [,int $ depth = 512 [,int $ options = 0]]])
<强> ASSOC 强> 如果为TRUE,则返回的对象将转换为关联数组。
第二个选项是JSON_OBJECT_AS_ARRAY,其效果与将assoc设置为TRUE相同。
要解决您遇到问题的部分应该能够用=====
替换 // check if an id came back
if (!empty($output->id)) {
$deal_id = $output->id;
return $deal_id;
} else {
return false; // should echo something instead of returning.
}
echo $deal_id;
下的内容。
echo 'response ok!';
var_export($decoded->response);
同样要删除其他输出,请注释这两行。
{{1}}