我使用 GraphQL/curl 返回了數據,如下所示:
{ "data" : { "publisher" : { "contracts" : { "totalCount" : 11, "count" : 1, "resultList" : [
我想要取得 resultList 數組,並在嘗試執行 $result->data 移動到第一個物件時不斷收到錯誤「警告:嘗試讀取字串上的屬性「資料」」。我做錯了什麼?
curl 請求中的變數是 $result。
更新:我已經嘗試解碼,返回的資料是 INT 類型?怎麼辦?
function getData($data_String){ $endpoint = "https://programs.api.cj.com/query"; $authToken = "pass"; $qry = '{"query":"{ publisher { contracts(publisherId: \"xxxxxxx\", limit: 1, filters: {advertiserId: \"'.$advertiser_id.'\"}) { totalCount count resultList { startTime endTime status advertiserId programTerms { id name specialTerms { name body } isDefault actionTerms { id actionTracker { id name description type } lockingMethod { type durationInDays } performanceIncentives {mission missionmission mission nmission value} mission values {mission missionmissions {mission nmission value} mission values {mission values {mission missionvalues {mission values {mission missionvalues {mission missionvalue.com 類型id name } itemList { id name } promotionalProperties { id name } rate { type value currency } } } } } } }","variables":null}'; $headers = array(); $headers[] = 'Content-Type: application/json'; $headers[] = 'Authorization: Bearer '.$authToken; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $endpoint); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); curl_setopt($ch, CURLOPT_POSTFIELDS, $qry); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } curl_close($ch); $data = json_decode($result); return $data; }
首先,請務必檢查結果是否是有效的 json。
然後使用json_decode取得物件