如何创建PHP cURL POST请求JSON

时间:2019-02-19 16:22:41

标签: php php-curl

我使用PHP cURL向特定的URL发出POST请求,并作为响应获取JSON对象。

由于我不确定是否可以提及我要连接的页面名称。 我正在从Firefox Page Inspector提供打印屏幕,以显示POST请求的JSON的外观以及我的代码如何创建一个。

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);

我不确定我做错了什么。我得到了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"}

0 个答案:

没有答案