Reputation: 57
In my code my Regexp
works fine when I assign html
content to variable but not working with url path
. I am getting empty array.
<?php
$productmfgno = "154637401";
$url = "http://www.pandorasoem.com/search#q=".$productmfgno;
$ch1= curl_init();
curl_setopt ($ch1, CURLOPT_URL, $url );
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1,CURLOPT_VERBOSE,1);
curl_setopt($ch1, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)');
curl_setopt ($ch1, CURLOPT_REFERER,'http://www.google.com'); //just a fake referer
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1,CURLOPT_POST,0);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, 20);
$htmlContent= curl_exec($ch1);
curl_close($ch1);
/* It works when I assign this html content to $htmlContent variable but not working with cURL url
$htmlContent = '<div class="findify-navigation-header findify-clearfix"> <div class="findify-pagination findify-push-right"></div> <div class="findify-header">Showing 2 results for <span class="findify-query">"154637401"</span>. <span id="findify-didyoumean"></span></div> </div>';
*/
preg_match_all('/<div.*class=\"findify\-header\".*?>(.*?)<span.*class=\"findify-query\">.*?<\/div>/Us', $htmlContent, $count);
print_r($count);
Expected Result - Showing 2 results for
So I can fetch that result count.
Upvotes: 0
Views: 233
Reputation: 159
Thing is, there are no results on the page, you're requesting. Actual search is performed via ajax after the page is loaded.
Ajax endpoint for the search, you might be looking for, returns result in javascript code (not json). There it is:
UPD: As the format is different you'll need a new regular expression. Something like this will do:
preg_match_all('/["\']?totalHits["\']?\s*:\s*(\d+)/gi', $htmlContent, $count);
Upvotes: 1