user3487256
user3487256

Reputation: 13

Curl with Loop using php

I have a problem, I want to get data from another website and display it on my website using cURL.

For example, this is data from the other website which I would like to parse and display on my website.

<div class="users-name"> <a href='#'>User 1</a></div>
<div class="users-name"> <a href='#'>User 2</a></div>
<div class="users-name"> <a href='#'>User 3</a></div>
<div class="users-name"> <a href='#'>User 4</a></div>
<div class="users-name"> <a href='#'>User 5</a></div>
<div class="users-name"> <a href='#'>User 6</a></div>
<div class="users-name"> <a href='#'>User 7</a></div>
<div class="users-name"> <a href='#'>User 8</a></div>

Currently I'm getting first row of data, but now I want to get all of this data.
I have tried this so far:

$curl = curl_init('www.domain.com/search.php');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

$page = curl_exec($curl);

if(curl_errno($curl)) // check for execution errors
{
    echo 'Scraper error: ' . curl_error($curl);
    exit;
}

curl_close($curl);

$regex = '/<div class="users-name">(.*?)<\/div>/s';

if(preg_match($regex, $page, $list))
    print_r($list[0]);
else
    print "Not found";

Upvotes: 0

Views: 140

Answers (1)

Ritesh Chandora
Ritesh Chandora

Reputation: 8640

Replace

preg_match($regex, $page, $list)

with

preg_match_all($regex, $page, $list)

preg_match stops after the first match. preg_match_all gets all the matches.

Upvotes: 1

Related Questions