【问题标题】:Graphql API response return unexpeted dataGraphql API 响应返回意外数据
【发布时间】:2020-07-10 10:46:26
【问题描述】:

我正在尝试使用 graphql API 调用从 expedia.com 获取酒店评论,我有 graphql 查询及其变量

当我尝试使用 postman 时,它返回正确的结果,但是当我尝试使用 cURL php 时,它什么也没返回

这是我的网址

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 格式

请帮我解决这个问题

【问题讨论】:

  • 请发布您的代码,请求正文。
  • @PrabhjotSinghKainth 行数据是我使用 POST 方法的请求正文
  • 尝试从 Postman 生成代码 sn-p 并比较您的代码。您发送的内容类型是否正确?
  • 是的,我已经这样做了,但它没有返回任何数据,如果 php 代码正常工作,请在此处发布该代码
  • 请求在操场上为我工作并在命令行上使用 cURL - 正如之前的评论者所要求的,请发布代码,因为问题必须存在。

标签: php curl graphql graphql-js express-graphql


【解决方案1】:

您的 GraphQL 查询出现错误,我不确定具体是什么,但我使用了 GraphQL Playground 并选择了“Copy 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 => '{"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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 2016-10-27
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 1970-01-01
    相关资源
    最近更新 更多