我知道在這個論壇上已經多次問過這種型別的問題,但是我查看了該問題的無數變體,并嘗試了許多不同的解決方案,但都沒有奏效,所以我想我會粘貼我的實際代碼,以防萬一我完全錯過了一些東西。所以我正在訪問 CRM 的 API 并接收 json 輸出。輸出如下所示:
{"access_token":"415db7610058604900566bdd","expires":1639406913,"expires_in":3600,"scope":"granted_permission","domain":"bitrix24.com","server_endpoint":"https:\/\/oauth.bitrix.info\/rest\/","status":"L","client_endpoint":"https:\/\/bitrix24.com\/rest\/","member_id":"**************","user_id":8,"refresh_token":"31dcde610058604900566bdd00000008f0f1037319823fdbcad5b438d3098146949075"}
這是我在使用 cURL 發布一個 URL 后得到的輸出,然后允許生成一個 refresh_token,可以在 json 輸出中看到。我必須決議 json 以獲取新的重繪 令牌,以便在令牌過期時可以重新輸入它。
經過研究,我了解到您可以使用 json_decode 函式通過 PHP 決議 json,我嘗試了許多不同的方法,嘗試使用 trim、true、stripslashes 和更多變體的函式。
當我運行下面的代碼時,我可以回顯 $result 變數并查看 json,但是當我嘗試只回顯從 json 輸出決議的 refresh_token 時,無論我嘗試哪種方法,我得到的只是一個空白螢屏并使用或即使我 var_dump。如果有人能解釋我做錯了什么,我是否應該匯入某種外部庫,或者我是否在某處錯過了一步,那將不勝感激。
<?php header("Content-Type: application/json; charset=UTF-8");
ini_set("allow_url_fopen", 1);
$client_id = "**********";
$secret_id = "****************";
$refresh_token = "94dade610058604900566bdd00000008f0f103f252c645ad3779a511ca2346b2fe9f27";
$refresh_token2 = "";
if($refresh_token2 == "") {
$url = "https://bitrix24.com/oauth/token/?grant_type=refresh_token&client_id=" . $client_id . "&client_secret=" . $secret_id . "&refresh_token=" . $refresh_token . "&scope=granted_permission&redirect_uri=app_URL";
// Initialize a CURL session.
$ch = curl_init();
// Return Page contents.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//grab URL and pass it to the variable.
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
$json = file_get_contents($result);
$obj = json_decode($json, true);
var_dump($obj);
}
在這里,我嘗試了@dietcheese 解決方案。我還添加了輸出的螢屏截圖,顯示它如何不輸出 json 值。這就是我之前嘗試過在論壇上發布的解決方案(@dietcheese 解決方案是其中之一)的意思,但它們不起作用。
<?php header("Content-Type: application/json; charset=UTF-8");
ini_set("allow_url_fopen", 1);
$client_id = "**************";
$secret_id = "**************";
$refresh_token = "250be0610058604900566bdd";
$refresh_token2 = "";
if($refresh_token2 == "") {
$url = "https://bitrix24.com/oauth/token/?grant_type=refresh_token&client_id=" . $client_id . "&client_secret=" . $secret_id . "&refresh_token=" . $refresh_token . "&scope=granted_permission&redirect_uri=app_URL";
// Initialize a CURL session.
$ch = curl_init();
// Return Page contents.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//grab URL and pass it to the variable.
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
echo "This is the result variable being echoed: \n\n" . $result;
curl_close($ch);
$json = json_decode($result, true);
echo "\n\n\nHere the json should print: \n\n";
print_r($json);
echo "\n\nHere the refresh token should echo: \n\n";
echo $json['refresh_token'];
}
下面是輸出:

這就是我問這個問題的原因,因為我真的很困惑什么都不起作用。
uj5u.com熱心網友回復:
你為什么用
$json = file_get_contents($result);
在 curl_close($ch) 陳述句之后,嘗試添加:
$json = json_decode($result, true);
print_r($json);
如果可行,您應該可以執行以下操作:
echo $json['refresh_token'];
uj5u.com熱心網友回復:
所以我發現了這個問題。感謝@álvaroGonzález 提醒我應該在我的代碼框中激活除錯。(我知道菜鳥的舉動,但我還是菜鳥)。
開啟除錯后,發現PHP連json_decode都認不出來了。然后為 PHP 安裝 JSON 模塊后,一切正常。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/383882.html
