How to get a certain html tag from an external site?

Asked

Viewed 99 times

-1

I made the following code below

<?php

    $url = "https://pt.clubcooee.com/settings/changename?token=24075208-1660433091-a355f7ce9bc39f2e8a2a09f30107ca3d&ms=1";

    $dados = file_get_contents($url);

    $var = explode('<h3 class="font-m">', $dados);
    $var2 = explode('</div>', $var[1]);

    print '<h3 class="font-m">'.$var2[1];

Obs.: Sometimes it is necessary to change the end of the url "ms=1" to "ms=2", in order to be able to log into the page!

The part that is selected in the image above is what I want to capture! However, always returns me the following error:

Notice: Undefined offset: 1 in D: Programas xamp htdocs trade index.php on line 8 Notice: Undefined offset: 1 in D: Programas xamp htdocs trade index.php on line 10

1 answer

-3


Not recommended! but I used the function preg_match_all

For that I created a simple Pattern, to obtain the respective values. However, with only the function file_get_contents() could not locate this data, because the site redirects to login area, because it does not contain the sessions and/or cookies in the request. That’s why I created a call curl for the given url. In fact it works! even without logging in to the page, as follows:

$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => "https://pt.clubcooee.com/settings/changename?token=24075208-1660433091-a355f7ce9bc39f2e8a2a09f30107ca3d&ms=1",
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_HTTPHEADER => array(
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
        "Connection: keep-alive",
        "Upgrade-Insecure-Requests: 1",
        "Host: pt.clubcooee.com",
        "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.67"
    ),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_COOKIESESSION => true,
    CURLOPT_COOKIEFILE => "cookie.log",
    CURLOPT_COOKIEJAR => "cookie.log",
    CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4
));

After the above code, I created a variable called $dados to execute Curl parameters.

$dados = curl_exec($ch);

Next comes the function preg_match_all

preg_match_all('/<h3 class="font-m">([^<]*)<\/h3>([^.\r\n]*)<\/div>/', $dados, $matches, PREG_SET_ORDER);

I used the flag PREG_SET_ORDER for the matrix data to be returned in an orderly manner!

However, just print the result on the page!

echo trim(strip_tags($matches[0][0]));

the function strip_tags() serves to remove html tags present between texts, such as <br> and etc..

"Your name changes so far 06/08/21 21:25 testbug01425451 -> testbug00071"

  • Good, I did all the code and still presents the error: Notice: Undefined offset: 0 in index.php on line 28

  • @LODSX tell me line 28

  • I made some adjustments here and got, thank you very much friend! xD

Browser other questions tagged

You are not signed in. Login or sign up in order to post.