PHP Getting text/value from a span by id and put it in a variable from an external site with cURL

13,126

To work with $_POST data, you actually have to post the content, typically in a form. The only way to do what you want is to fetch and extract the data from the HTML file itself. (file_get_contents and curl may come in handy at this point)

Finally, you're going to have to parse and extract the data from your obtained content. There are a handful of ways you could do this including using a regular expression or by working directly with the Document Object Model (DOM). (I prefer the latter method.)

Share:
13,126
Jordy144
Author by

Jordy144

Updated on June 25, 2022

Comments

  • Jordy144
    Jordy144 almost 2 years

    My question is, how do I get the value/text by id, thats between a span from an external website by using cURL. And after that, put it in a variable.

    I have the following span:

    <span id="company" class="company">Example</span>
    

    I tried to use:

    $company = $_POST['company'];
    echo($company);
    

    But that didn't work.

    The output should be:

    Example
    
  • Jordy144
    Jordy144 over 10 years
    Hi Carson, thanks for your reply. I am using curl, sorry for not mentioning it. The span is from an external website.
  • Jordy144
    Jordy144 over 10 years
    Hi Louis, thanks for your reply. Yes I am using curl. I want to get the text/value from a span that's on an external website.
  • Carson Myers
    Carson Myers over 10 years
    Whoops, didn't see that. In that case go with Louis' answer - and take his advice and use a DOM parser, don't use regular expressions (see this: stackoverflow.com/a/1732454/84478)
  • Julio
    Julio over 10 years
    So curl the page's content, and parse out the data by working with the DOM.