Jump to content

[SOLVED] Displaying specific information from an external site


Someone789

Recommended Posts

Hello,

I've been crawling through Google for hours trying to find a solution, but have yet to come up with anything workable. And after searching for a nice PHP help forum, well here I am. My apologies if this is the wrong category - I'm absolutely completely new to PHP, but from what I've read elsewhere, I believe what I want to do involves Regex.

 

I was wondering how it would be possible to grab a small bit of HTML from an external site (which I have no control over), and place it in a specific area on one of my pages. Example: An external price list page contains the following HTML:

<span class="text-breaker">
<b>Sale price:</b> $5
</span>

 

Now on my own page, I would like to have a bit of text that displays as "And the current sale price of this item is [insert pulled sale price from the external site(currently $5)]"

 

I'm sure there's got to be some simple explanation, but how would this be done?

 

Much thanks in advance!

 

 

Link to comment
Share on other sites

Is there only one value you want to pull? If not, change preg_match to preg_match_all

 

<?php

// $subject = file_get_contents('http://remote.file/somefile.php');
$subject = '<span class="text-breaker">
<b>Sale price:</b> $5
</span>';

$regex = '%<span class="text-breaker">\s++<b>Sale price:</b> \$([\d]++)\s++</span>%';

preg_match($regex, $subject, $match);

print_r($match);

?>

Link to comment
Share on other sites

Thanks for the quick reply. However, I'm afraid that didn't do it. All I get is this in that area when I view the page:

Array ( ) 

And when I tried preg_match_all instead of just preg_match, it came out as this:

Array ( [0] => Array ( ) [1] => Array ( ) ) 

 

Possibly, could it be that the page I'm pulling it from doesn't end in a .php extension? It ends like this: "/viewitem.ws?obj=554" (http://[site Here].com/viewitem.ws?obj=554)

 

Oh, and another thing I believe that might be hindering this is that I accidentally gave slightly wrong information - It doesn't appear as "$5" but instead, just "5", so it would be:

<span class="text-breaker">
<b>Sale price:</b> 5
</span>

Link to comment
Share on other sites

Much thanks for the help - though I'm afraid that still didn't do it either. Hm, maybe I'm just going about this incorrectly. Let me just post what I have so far - The bit of coding from the external site I want to pull is:

<span class="text-breaker">
<b>Sale price:</b> 5
</span>

(And of course that sale price number will change every so often, which is why I was hoping to have it updated manually on my page)

 

Here's my bit of code that I put on my page to display that current price:

<?php

// $subject = file_get_contents('http://[External Site].com/viewitem.ws?obj=882');
$subject = '<span class="text-breaker">
<b>Sale price:</b> 5
</span>';

$regex = '%<span class="text-breaker">\s++<b>Sale price:</b> ([\d]++)\s++</span>%';

preg_match($regex, $subject, $match);

print_r($match);

?>

 

However, this is all I get in that area when I view the page in a browser:

Array ( [0] => Sale price: 5  [1] => 5 )

 

 

 

Link to comment
Share on other sites

 

However, this is all I get in that area when I view the page in a browser:

Array ( [0] => Sale price: 5  [1] => 5 )

 

 

Hmm I'm sorry. I don't know what you are trying to do anymore. The code you get when you do a var_dump is the actual price... wasn't it the price you wanted to retrieve? Just use the variabel $match[1], it should allways contain the price. Then you can print it, echo it, store it in xml, store it in datbase... in other words do whatever you wan't with it :-)

 

If it is not the price you are after, could you please try and explain again what you need?

Link to comment
Share on other sites

Thanks again for the reply - Sorry, I have a strong feeling that either I'm just not doing something that's extremely obvious/easy or I did a poor job of explaining what I want to do. I'm very new to php and this is the first time I've worked with something like this (*shrugs shoulders in embarrassment*).

 

I need to pull a value (a price) from an external site that I have no control over to be displayed on my own page. I would just type this number out myself in regular HTML, but since the price changes frequently, I was hoping that I could use php to have it update itself (instead of me manually editing the price each time it changed) by just pulling the price directly off the external page each time.

 

Here is the code I now have:

<?php

// $subject = file_get_contents('http://[External Site].com/viewitem.ws?obj=882');
$subject = '<span class="text-breaker">
<b>Sale price:</b> 5
</span>';

$regex = '%<span class="text-breaker">\s++<b>Sale price:</b> ([\d]++)\s++</span>%';

preg_match($regex, $subject, $match);

echo $match[1];
?>

 

I used the $match[1] variable like you said, and that area of my page when viewed in a browser nicely displays a "5". But here's the thing - The price on the external site was changed to a "4" today, but why does it still display as a "5" on my page? (My goal is to have the number on my page always automatically match the number on the external page, even if the number on the external page is changed).

Link to comment
Share on other sites

Uncomment this

 

// $subject = file_get_contents('http://[External Site].com/viewitem.ws?obj=882');

 

Remove this

 

$subject = '<span class="text-breaker">
<b>Sale price:</b> 5
</span>';

 

That should do the trick.

Link to comment
Share on other sites

On second thought - I notice this script is taking a very long time to load (~30 seconds for pulling only one number, and much more time for the more numbers I pull).

 

Would there somehow be a way that I could pull the numbers at a quicker rate, or will this just be something I'll have to live with?

 

Note: Here's what I'm using now to add in multiple values:

<?php

$subject = file_get_contents('http://[External Site].com/viewitem.ws?obj=554');
$regex = '%<span class="text-breaker">\s++<b>Sale price:</b> ([\d]++)\s++</span>%';

$subject1 = file_get_contents('http://[External Site].com/viewitem.ws?obj=555');
$regex1 = '%<span class="text-breaker">\s++<b>Sale price:</b> ([\d]++)\s++</span>%';

preg_match($regex, $subject, $match);
preg_match($regex1, $subject1, $match1);

echo "$match[1]<br>";
echo $match1[1]; 

?>

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.