简体   繁体   English

如何将参数发送到另一个PHP网站的AJAX POST方法并获取JSON信息

[英]How to send parameters to AJAX POST method of another PHP website and fetch JSON information

I am researching on " how to parse the content from other website ". 我正在研究“ 如何解析其他网站的内容 ”。 I am using HTML DOM parsing to fetch the information. 我正在使用HTML DOM解析来获取信息。 Problem i am facing is with websites which contains AJAX POST calls to fetch the information. 我面临的问题是包含AJAX POST调用以获取信息的网站。 Example website: Massachussets 示例网站: 马萨诸塞州

1.Here doctors information is fetched using ajax post method 1.这里医生的信息是使用ajax post方法获取的

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

How do i pass the parameters to post methods here?. 我如何在此处传递参数以发布方法? What i tried is 我试过的是

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

It did not fetched the doctors information. 它没有获取医生的信息。 Please provide links or idea to parse it. 请提供链接或想法进行解析。

You're using http_build_query() which creates a "foo=1&bar=2" style format. 您正在使用http_build_query()创建了“ foo = 1&bar = 2”样式格式。 The site is expecting json so you want to use json_encode() instead. 该站点需要json,因此您想改用json_encode()

Also, leave out the gzip header unless you're sure you really want to deal with a gzipped response. 另外,除非确定您确实要处理gzip压缩的响应,否则请忽略gzip标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM