【问题标题】:How to send parameters to AJAX POST method of another PHP website and fetch JSON information如何向另一个 PHP 网站的 AJAX POST 方法发送参数并获取 JSON 信息
【发布时间】:2018-01-25 11:40:06
【问题描述】:

我正在研究“如何解析来自其他网站的内容”。我正在使用 HTML DOM 解析 来获取信息。我面临的问题是包含 AJAX POST 调用以获取信息的网站。示例网站:Massachussets

1.这里使用ajax post方式获取医生信息

Request URL:http://www.massgeneral.org/assets/javascripts/facets/doctors/doctors.ashx
Request Method:POST

如何将参数传递给此处的发布方法?我尝试的是

<?php
echo '<center><h3>Massachusetts Information</h3></center>';
// extra headers
//$headers[]= "Accept-Encoding: gzip, deflate";
$fields['center'] = "";
$fields['centerPreSelected'] = false;
//$fields['displayPaging'] = false;
$fields['gender'] = "";
$fields['isEmpty'] = true;
$fields['languages'] = [];
$fields['letter'] = "A";
$fields['letter'] = "";
$fields['locations'] = array();
$fields['numberOfPages'] = 15;
$fields['numberPerPage'] = 50;
$fields['page'] = 1;
$fields['program'] = "";
$fields['range'] = array('Item1' => 0,'Item2' => 49);
$fields['saytLimit'] = "20";
$fields['term'] = "";

$POSTFIELDS = http_build_query($fields);

$headers[] = "Accept: application/json, text/plain, */*";
$headers[] = "Accept-Encoding: gzip, deflate";
$headers[] = "Accept-Language: en-GB,en;q=0.5";
$headers[] = "Connection: keep-alive";
$headers[] = "Content-Type: application/json";
$headers[] = "Host: www.massgeneral.org";
$headers[]="Referer: http://www.massgeneral.org/doctors/";

$login_submit_url = "http://www.massgeneral.org/assets/javascripts/facets/doctors/doctors.ashx";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER,  0);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_URL, $login_submit_url);
curl_setopt($ch, CURLOPT_HTTPHEADER,  $headers);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
$result = curl_exec($ch);

echo $result;

它没有获取医生信息。 请提供链接或想法来解析它。

【问题讨论】:

  • 您想在该信息中提取什么?你试过DOMDocument吗?
  • 不。这是我第一次听说。我想从那个站点检索所有医生信息。
  • 如果我理解正确,您正试图抓取网站的内容,但该网站使用 AJAX 填充其内容。因此,当您使用 cURL 获取网站的内容时,它缺少由 AJAX 填充的内容,您想知道如何获取它.. 对吗?
  • @Nick Coons .Exactly !...我想我在调用 AJAX POST 时缺少一些要发送的参数
  • 您是要对提前知道 AJAX 调用的特定站点执行此操作,还是对可能包含此类调用的任何给定站点执行此操作?

标签: php ajax dom html-parsing simple-html-dom


【解决方案1】:

您正在使用http_build_query() 创建“foo=1&bar=2”样式格式。该站点需要 json,因此您想改用 json_encode()

此外,请忽略 gzip 标头,除非您确定确实要处理 gzip 压缩响应。

【讨论】:

  • 当我用 json_encode() 替换 http_build_query() 时,它会返回如下输出:���I�%&amp;/m�{J�J��t��$ؐ@������iG#)�*��eVe]f@ �흼��{���{���;�N'���?\fdl��J�ɞ!���?....... .你能建议我在我的代码中需要在哪里进行更改吗?
  • 取出gzip标头。它会向您发送压缩响应,因为这正是您所要求的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
  • 2015-12-16
  • 2012-12-25
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 2015-10-03
相关资源
最近更新 更多