简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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