我是 Visa 開發人員平臺 (VDP) API 的新手,我在嘗試使用 php 將回應的輸出讀取為 json 時遇到了問題。我跟著教程here。我正在使用優惠資料 API。
我的目標是能夠生成要在前端顯示的交易串列。我試圖通過讀取 json 輸出并決議交易資訊來做到這一點。
這是原始教程的內容,并且效果很好:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_PORT, 443);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
curl_setopt($curl, CURLOPT_SSLCERT, $cert);
curl_setopt($curl, CURLOPT_SSLKEY, $key);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl); //This output contains the json output as well
$response_info = curl_getinfo($curl);
為了獲取資訊,我正在運行:$response_data = file_get_contents($response);這似乎不起作用。由于輸出不僅是 json,而且還有其他資訊,因此我無法運行: $arr = json_decode($response,true);決議 json。這是 json 的樣子:
{"ReturnedResults":4,"StartIndex":1,"Offers":[{"indexNumber":1,"offerContentId":105515,"offerId":
等等。json 開始{"indexNumber":1于它需要丟棄之前的所有內容。請讓我知道我可以做些什么來解決這個問題,或者是否有更好的方法來實作目標。謝謝!
uj5u.com熱心網友回復:
使用CURLOPT_RETURNTRANSFER => true-初始化 cURL,然后您的內容將處于$response可變狀態。如果沒有那個選項,結果curl_exec總是布林值。
uj5u.com熱心網友回復:
目標
json 以 {"indexNumber":1 開頭以及需要丟棄之前的所有內容。請讓我知道我可以做些什么來解決這個問題,或者是否有更好的方法來實作目標。
代碼
回應變數包含一個有效的 json 物件。由于您只需要Offers您可以使用此代碼來獲取Offers:
$response = '{"ReturnedResults":4,"StartIndex":1,"Offers":[{"indexNumber":1,"offerContentId":105515,"offerId":""}]}';
$json = json_decode($response, true);
var_dump($json['Offers']);
輸出
array(1) {
[0]=>
array(3) {
["indexNumber"]=>
int(1)
["offerContentId"]=>
int(105515)
["offerId"]=>
string(0) ""
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/359706.html
