我是Curl的初學者,我對從curl_exec()得到的值有問題。
我正在連接一個銀行的服務器,并且想從服務器上只得到陣列,但是curl_exec()回傳整個字串的頭和陣列。我怎樣才能讓它只發送陣列呢?
<?php
$url = 'https://the-url.com'/span>;
$auth = '認證密鑰'。
$method ='POST'。
$grant ='grant_type=client_credentials'。
$crl = curl_init()。
$headr = array();
$headr[] = 'Content-type: application/x-www-form-urlencoded'/span>;
$headr[] = 'Authorization: Basic'.$auth;
curl_setopt($crl, CURLOPT_URL,$url) 。
curl_setopt($crl, CURLOPT_HTTPHEADER,$headr) 。
curl_setopt($crl, CURLOPT_POST,true)。
curl_setopt($crl, CURLOPT_HEADER, true)。
curl_setopt($crl, CURLOPT_RETURNTRANSFER, true)。
curl_setopt($crl, CURLOPT_POSTFIELDS, $grant) 。
$rest = curl_exec($crl) 。
if ($rest ==false)
{
print_r('Curl error: ' . curl_error($crl) )。)
}
curl_close($crl)。
$data = json_decode($rest, true) 。
echo $rest;
? >
這個輸出
HTTP/2 200 a: x content type: application/json content-length: 345 日期: Tue, 21 Sep 2021 12: 26:47 GMT strict-transport-security: max-age=63072000{array}。
當我想讓它只輸出{array}時,我可以對它使用json_decode()
uj5u.com熱心網友回復:
嘗試改變這一行
$headr[] = 'Content-type: application/x-www-form-urlencoded'/span>;
to
$headr[] = 'Content-type: application/json'/span>;
因為你期待的是一個json回應。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/331027.html
標籤:
上一篇:Django創建帖子并發送郵件
