Ответ API Graphql возвращает неисследованные данные - PullRequest
0 голосов
/ 30 марта 2020

Я пытаюсь получить обзор отелей от expedia.com, используя API-вызов graphql, у меня есть запрос graphql и его переменная

, когда я пытаюсь с почтальоном вернуть правильный результат, но когда я пытаюсь с cURL php ничего не возвращается

вот мой URL

https://www.expedia.com/graphql

вот мои данные строки для почтового запроса

{
  "operationName": "Reviews",
  "query": "query Reviews($context: ContextInput!, $propertyId: String!, $pagination: PaginationInput!, $sortBy: PropertyReviewSort!, $filters: PropertyReviewFiltersInput!) {propertyInfo(propertyId: $propertyId,  context: $context) {reviewInfo(sortBy: $sortBy, pagination: $pagination, filters: $filters) { summary { superlative totalCount { raw formatted } reviewCountLocalized averageOverallRating { raw formatted } cleanliness { raw formatted } serviceAndStaff { raw formatted } amenityScore { raw formatted } hotelCondition { raw formatted } cleanlinessPercent cleanlinessOverMax serviceAndStaffPercent serviceAndStaffOverMax amenityScorePercent amenityScoreOverMax hotelConditionPercent hotelConditionOverMax ratingCounts { count { formatted raw } percent rating } lastIndex reviewDisclaimer } reviews { id ratingOverall superlative submissionTime { raw } title text locale author userLocation stayDuration helpfulReviewVotes negativeRemarks positiveRemarks locationRemarks photos { description url } managementResponses { id date displayLocale userNickname response } travelers themes { icon { id description } label } } sortAndFilter { sortAndFilter { name label options { label isSelected optionValue } } } } } }",
  "variables": {
    "context": {
      "siteId": 1,
      "locale": "en_US",
      "currency": "USD",
      "device": {
        "type": "DESKTOP"
      },
      "identity": {
        "duaid": "986e9653-0e36-413f-8fff-19bd7bddbe9b",
        "expUserId": "-1",
        "tuid": "-1",
        "authState": "ANONYMOUS"
      },
      "debugContext": {
        "abacusOverrides": [

        ],
        "alterMode": "RELEASED"
      }
    },
    "propertyId": "1173275",
    "sortBy": "NEWEST_TO_OLDEST",
    "filters": {
      "includeRecentReviews": false,
      "includeRatingsOnlyReviews": true
    },
    "pagination": {
      "startingIndex": 10,
      "size": 10
    }
  }
}

вот мой php код cURL

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://www.expedia.com/graphql",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\n  \"operationName\": \"Reviews\",\n  \"query\": \"query Reviews($context: ContextInput!, $propertyId: String!, $pagination: PaginationInput!, $sortBy: PropertyReviewSort!, $filters: PropertyReviewFiltersInput!) {propertyInfo(propertyId: $propertyId,  context: $context) {reviewInfo(sortBy: $sortBy, pagination: $pagination, filters: $filters) { summary { superlative totalCount { raw formatted } reviewCountLocalized averageOverallRating { raw formatted } cleanliness { raw formatted } serviceAndStaff { raw formatted } amenityScore { raw formatted } hotelCondition { raw formatted } cleanlinessPercent cleanlinessOverMax serviceAndStaffPercent serviceAndStaffOverMax amenityScorePercent amenityScoreOverMax hotelConditionPercent hotelConditionOverMax ratingCounts { count { formatted raw } percent rating } lastIndex reviewDisclaimer } reviews { id ratingOverall superlative submissionTime { raw } title text locale author userLocation stayDuration helpfulReviewVotes negativeRemarks positiveRemarks locationRemarks photos { description url } managementResponses { id date displayLocale userNickname response } travelers themes { icon { id description } label } } sortAndFilter { sortAndFilter { name label options { label isSelected optionValue } } } } } }\",\n  \"variables\": {\n    \"context\": {\n      \"siteId\": 1,\n      \"locale\": \"en_US\",\n      \"currency\": \"USD\",\n      \"device\": {\n        \"type\": \"DESKTOP\"\n      },\n      \"identity\": {\n        \"duaid\": \"986e9653-0e36-413f-8fff-19bd7bddbe9b\",\n        \"expUserId\": \"-1\",\n        \"tuid\": \"-1\",\n        \"authState\": \"ANONYMOUS\"\n      },\n      \"debugContext\": {\n        \"abacusOverrides\": [\n          \n        ],\n        \"alterMode\": \"RELEASED\"\n      }\n    },\n    \"propertyId\": \"1173275\",\n    \"sortBy\": \"NEWEST_TO_OLDEST\",\n    \"filters\": {\n      \"includeRecentReviews\": false,\n      \"includeRatingsOnlyReviews\": true\n    },\n    \"pagination\": {\n      \"startingIndex\": 10,\n      \"size\": 10\n    }\n  }\n}",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "content-type: application/json",
    "postman-token: 1a092745-0b00-5abb-3680-8fcd30eae915"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Я также попытался с помощью node js запроса вызова, но его возвращаемые данные в буфере и эти данные не удалось преобразовать в json формат

. Пожалуйста, помогите мне решить эту проблему

1 Ответ

0 голосов
/ 31 марта 2020

В вашем запросе GraphQL есть ошибка, я не совсем уверен, что именно, но я использовал GraphQL Playground и выбрал «Копировать завиток» и вставил запрос в ваш код, и это работает для меня:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://www.expedia.com/graphql",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => '{"query":"query Reviews($context: ContextInput!, $propertyId: String!, $pagination: PaginationInput!, $sortBy: PropertyReviewSort!, $filters: PropertyReviewFiltersInput!){\n  propertyInfo(propertyId: $propertyId, context: $context) {\n    reviewInfo(sortBy: $sortBy, pagination: $pagination, filters: $filters) {\n      summary {\n        superlative\n        totalCount {\n          raw\n          formatted\n        }\n        reviewCountLocalized\n        averageOverallRating {\n          raw\n          formatted\n        }\n        cleanliness {\n          raw\n          formatted\n        }\n        serviceAndStaff {\n          raw\n          formatted\n        }\n        amenityScore {\n          raw\n          formatted\n        }\n        hotelCondition {\n          raw\n          formatted\n        }\n        cleanlinessPercent\n        cleanlinessOverMax\n        serviceAndStaffPercent\n        serviceAndStaffOverMax\n        amenityScorePercent\n        amenityScoreOverMax\n        hotelConditionPercent\n        hotelConditionOverMax\n        ratingCounts {\n          count {\n            formatted\n            raw\n          }\n          percent\n          rating\n        }\n        lastIndex\n        reviewDisclaimer\n      }\n      reviews {\n        id\n        ratingOverall\n        superlative\n        submissionTime {\n          raw\n        }\n        title\n        text\n        locale\n        author\n        userLocation\n        stayDuration\n        helpfulReviewVotes\n        negativeRemarks\n        positiveRemarks\n        locationRemarks\n        photos {\n          description\n          url\n        }\n        managementResponses {\n          id\n          date\n          displayLocale\n          userNickname\n          response\n        }\n        travelers\n        themes {\n          icon {\n            id\n            description\n          }\n          label\n        }\n      }\n      sortAndFilter {\n        sortAndFilter {\n          name\n          label\n          options {\n            label\n            isSelected\n            optionValue\n          }\n        }\n      }\n    }\n  }\n}","variables":{"context":{"siteId":1,"locale":"en_US","currency":"USD","device":{"type":"DESKTOP"},"identity":{"duaid":"986e9653-0e36-413f-8fff-19bd7bddbe9b","expUserId":"-1","tuid":"-1","authState":"ANONYMOUS"},"debugContext":{"abacusOverrides":[],"alterMode":"RELEASED"}},"propertyId":"1173275","sortBy":"NEWEST_TO_OLDEST","filters":{"includeRecentReviews":false,"includeRatingsOnlyReviews":true},"pagination":{"startingIndex":10,"size":10}}}',
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "content-type: application/json",
    "postman-token: 1a092745-0b00-5abb-3680-8fcd30eae915"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
...