Как создать PHP-запрос cURL POST JSON - PullRequest
0 голосов
/ 19 февраля 2019

Я использую PHP cURL, чтобы сделать POST-запрос к определенному URL-адресу и в ответ получить объект JSON.

Поскольку я не был уверен, смогу ли я упомянуть имя страницы, к которой пытаюсь подключиться.Я предоставляю экран печати из Firefox Page Inspector, как должен выглядеть JSON для POST-запроса, а мой код - как его создавать.

enter image description here

$url = "https://www.***.com/graphql/";
$data = [
'clientVersion' => 'home-details/5.38.1.0.0.hotfix-2019-2-14.5b4b1fc',
'operationName' => 'ForRentFullRenderQuery',
'query' => 'query ForRentFullRenderQuery($zpid: ID!) { property(zpid: $zpid) { id
...HeaderElements_property ...HomeFactsFeatures_property
...RentalFooterSection_property ...ForRentComponentSection_property
...ForRentHomeMenu_property ...ForRentInlineMenuAndMap_property
...HomeSummary_property ...PriceArea_property ...OpenHouseCalendar_property
...HomeDescription_property ...PhotoDisplayContainer_property
...HomeDetailsNotification_property ...HomeHeaderRow_prop_viewer
...ForRentMobileMoreMenuContents_viewer } fragment InlineMenu_viewer on Viewer {
...SuperShareMenu_viewer } fragment SuperShareMenu_viewer on Viewer {
...Share_viewer } fragment Share_viewer on Viewer { email } fragment 
ForRentMobileMoreMenuContents_viewer on Viewer { ...EditRentalListing_viewer } 
fragment EditRentalListing_viewer on Viewer { zuid } fragment 
RentalFooterSection_viewer on Viewer { ...DebugFooter_viewer } fragment 
DebugFooter_viewer on Viewer { isAdmin zuid } ',
'variables' => array('contactFormRenderParameter' => array('platform' =>  
'desktop', 'zpid' => $zpid)),
'zpid' => $zpid
];

$data = json_encode($data);

$request_headers = [
'Host: www.***.com',
'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:64.0)
Gecko/20100101 Firefox/64.0',
'Content-Length: ' . strlen($data),
'Connection: keep-alive',
'content-type: application/json'
];

$curl = curl_init($url);

curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPGET, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

echo $result = curl_exec($curl);

IЯ не уверен, что я делаю неправильно.Я получаю 400 ответ и ошибку:

{"errors":[{"message":"Syntax Error: Expected :, found )","locations":[{"line":1,"column":35}],"extensions":{"code":"GRAPHQL_PARSE_FAILED"}}]}

Заранее спасибо за помощь.

Обновление: Вот вывод перед ошибкой:

{"clientVersion":"home-details\/5.38.1.0.0.hotfix-2019-2-14.5b4b1fc","operationName":"ForRentFullRenderQuery","query":"query ForRentFullRenderQuery($zpid) { property(zpid: $zpid) { id ...HeaderElements_property ...HomeFactsFeatures_property ...RentalFooterSection_property ...ForRentComponentSection_property ...ForRentHomeMenu_property ...ForRentInlineMenuAndMap_property ...HomeSummary_property ...PriceArea_property ...OpenHouseCalendar_property ...HomeDescription_property ...PhotoDisplayContainer_property ...HomeDetailsNotification_property ...HomeHeaderRow_prop_viewer ...ForRentMobileMoreMenuContents_viewer } fragment InlineMenu_viewer on Viewer { ...SuperShareMenu_viewer } fragment SuperShareMenu_viewer on Viewer { ...Share_viewer } fragment Share_viewer on Viewer { email } fragment ForRentMobileMoreMenuContents_viewer on Viewer { ...EditRentalListing_viewer } fragment EditRentalListing_viewer on Viewer { zuid } fragment RentalFooterSection_viewer on Viewer { ...DebugFooter_viewer } fragment DebugFooter_viewer on Viewer { isAdmin zuid } ","variables":{"contactFormRenderParameter":{"platform":"desktop","zpid":"2085794453"}},"zpid":"2085794453"}
...