2

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
  • 91
  • 1
  • 1
  • 7

2 Answers2

1

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.)

Community
  • 1
  • 1
Julio
  • 2,261
  • 4
  • 30
  • 56
  • 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. – Jordy144 Dec 19 '13 at 22:56
  • So `curl` the page's content, and parse out the data by working with the DOM. – Julio Dec 19 '13 at 22:59
0

Since you're using POST data I'm assuming you're submitting a form on the page already.

Part of your page probably looks like this:

<span id="company" class="company"><?php echo $company; ?></span>

Just add this to your form:

<form ...>
    <input type="hidden" name="company" value="<?php echo $company; ?>" />

    ...
</form>

Now when you submit the form $_POST['company'] will be populated.

Carson Myers
  • 37,678
  • 39
  • 126
  • 176
  • Hi Carson, thanks for your reply. I am using curl, sorry for not mentioning it. The span is from an external website. – Jordy144 Dec 19 '13 at 22:55
  • 1
    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: http://stackoverflow.com/a/1732454/84478) – Carson Myers Dec 19 '13 at 22:57