[英]How to convert guzzle psr7 response to an array
我希望檢索一個API
的內容並使用我的刀片中的內容。 如果我返回響應,我可以看到如下內容
{"CUSTOMER_NO":"00001292","VAL_1":"51610"}
但我無法從上述 output 中檢索CUSTOMER_NO
或VAL_1
字段。 也不允許將psr7
響應轉換為數組或字符串。 json_decode((string)$response->getBody,True)
也不返回任何東西。 我也不能直接在我的刀片中使用 $response 作為數組。
我的 API 代碼:
$sql="SELECT CUSTOMER_NO,VAL_1 from tablename where VAL_1=$test";
$stid = oci_parse($conn, $sql);
oci_execute($stid);
$line = oci_fetch_array($stid, OCI_ASSOC);
echo json_encode($line);
我的 Controller 代碼:
$client=new Client();
$url='http://test.com/test.php';
$response=($client->request('POST',$url,[ 'form_params' => [
'ac_no' => $ac_no,]],array()));
return $response;
我想分別取CUSTOMER_NO
、 VAL_1
的值/在刀片中使用相同的值。 請投一些光。
您需要使用->getBody()->getContents()
方法來獲取 json 響應。
$response=($client->request('POST',$url,[ 'form_params' => ['ac_no' => $ac_no,]],array()));
$response = $response->getBody()->getContents();
$res =json_decode($response,true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.