【发布时间】: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