Search code examples
phpajaxdomhtml-parsingsimple-html-dom

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. Problem i am facing is with websites which contains AJAX POST calls to fetch the information. Example website: Massachussets

1.Here doctors information is fetched using ajax post method

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.


Solution

  • You're using http_build_query() which creates a "foo=1&bar=2" style format. The site is expecting json so you want to use json_encode() instead.

    Also, leave out the gzip header unless you're sure you really want to deal with a gzipped response.