ホームページ  >  に質問  >  本文

curl を使用して GraphQL から返されたオブジェクトにアクセスする

次のように GraphQL/curl を使用してデータを返しました:

{
  "データ" : {
    「出版社」: {
      「契約」: {
        "合計数" : 11、
        「カウント」: 1、
        "結果リスト" : [

resultList 配列を取得したいのですが、$result->data を実行して最初のオブジェクトに移動しようとすると、「警告: 文字列のプロパティ 'data' を読み取ろうとしています」というエラーが発生し続けます。私が何を間違えたのでしょうか?

curl リクエストの変数は $result です。

更新: デコードを試みましたが、返されたデータは INT 型ですか?何をするか?

関数 getData($data_String){

    $endpoint = "https://programs.api.cj.com/query";
    $authToken = "パス";
    $qry = '{"query":"{ Publisher { Contracts(publisherId: \"xxxxxxx\", 制限: 1, フィルター: {advertiserId: \"'.$advertiser_id.'\"}) { totalCount count resultList { startTime endTime status adviceerId projectTerms { id namespecialTerms { name body } isDefault actionTerms { id actionTracker { id name description type } lockingMethod { type periodInDays } PerformanceIncentives {閾値 { type value } 報酬 { type CommissionType value } Currency } Commissions { ランク状況 { id name } itemList { ID 名 } PromotionProperties { ID 名 } rate { 型 値 通貨 } } } } } } }","変数":null}';

    $headers = 配列();
    $headers[] = 'コンテンツタイプ: application/json';
    $headers[] = '認証: ベアラー '.$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 'エラー:' .curl_error($ch);
    }

    カール_クローズ($ch);

    $data = json_decode($result);


    $data を返します。

}

P粉615829742P粉615829742210日前346

全員に返信(1)返信します

  • P粉955063662

    P粉9550636622024-02-27 14:54:39

    まず、結果が有効な json であるかどうかを必ず確認してください。

    次に、json_decode を使用してオブジェクトを取得します

    $結果 = json_decode($result);
    if (is_object($result)) {
      if (!empty($result->data->publisher->contracts->resultList)) {
         $resultList = $result->データ->発行者->契約->結果リスト;
      }
    } それ以外 {
      // ログか何か
       error_log("json デコードリターン: " .print_r($result, true))
    }

    返事
    0
  • キャンセル返事