0

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.

7
  • I am getting empty array. Commented Mar 15, 2016 at 6:07
  • pandorasoem.com/search#q=154637401 - this page source doesnt contain any html you mentioned, It is loading results by javascript Commented Mar 15, 2016 at 6:09
  • 1
    I see no class="findify-query" in view-source:http://www.pandorasoem.com/search#q=154637401 Commented Mar 15, 2016 at 6:10
  • 1
    But I am getting content when echo $htmlContent variable. Commented Mar 15, 2016 at 6:16
  • Is there any other way to get that number Commented Mar 15, 2016 at 6:17

1 Answer 1

1

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:

http://api.findify.io/v1.0/store/search?callback=jQuery111206735094679573879_1458022087824&q=154637401&key=5b31ee91-78fa-48e1-9338-1748ca55028e&analytics%5Bkey%5D=5b31ee91-78fa-48e1-9338-1748ca55028e&analytics%5Bvisit%5D=true&analytics%5Buniq%5D=true&analytics%5Burl%5D=http%253A%252F%252Fwww.pandorasoem.com%252Fsearch%2523q%253D154637401&analytics%5Bbaseurl%5D=http%253A%252F%252Fwww.pandorasoem.com%252Fsearch%2523q%253D154637401&analytics%5Bhost%5D=www.pandorasoem.com&analytics%5Bwidth%5D=1920&analytics%5Bheight%5D=1200&analytics%5Binner_width%5D=1438&analytics%5Binner_height%5D=667&analytics%5Bdoc_width%5D=1438&analytics%5Bdoc_height%5D=915&analytics%5Bscroll_x%5D=0&analytics%5Bscroll_y%5D=0&analytics%5Bvisit_id%5D=Ts22zuHHGJRZc3U1&analytics%5Buniq_id%5D=BoeCUKSzgdML6C50&byPage=24&page=0&_=1458022087825

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);
Sign up to request clarification or add additional context in comments.

5 Comments

I was about to post it
So I need to apply Regexp to this url ? I want to fetch products count from search result. so I can fetch data to each products.
Yes, you'll need to apply a regex to this url. But you'll need to rewrite it (see UPD).
first it shows unknown modifier g error then after removing g from regexp it shows empty array. what is totalHits here ?
@s1lent1um What will be the Regexp to extract product urls on the same page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.