Page 1 of 2
Can you query a Javascript Query?
Posted: Tue Sep 15, 2009 1:31 pm
by simonmlewis
Code: Select all
<SCRIPT LANGUAGE=\"JavaScript\" SRC=\"http://www.domain.com/data.asp?storeid=44175&itemcode=$row->shopcode&type=4\"></script>
This script queries a third party's server and the result can be one of a few responses, including "not currently available".
It's in a PHP page.
Is it possible to see what the result is (what it's going to show on the web page), and then action on that?
I tried assigning the query to a $variable, but it didn't work.
Re: Can you query a Javascript Query?
Posted: Tue Sep 15, 2009 1:45 pm
by requinix
Code: Select all
$contents = file_get_contents("http://www.domain.com/data.asp?storeid=44175&itemcode={$row->shopcode}&type=4");
if ($contents == "not currently available") {
// something
} else if ($contents == "something else") {
// something else
// etc
}
Re: Can you query a Javascript Query?
Posted: Tue Sep 15, 2009 2:14 pm
by simonmlewis
Code: Select all
$contents = file_get_contents("http://www.domain.com/data.asp?storeid=44175&itemcode={$row->shopcode}&type=4");
if ($contents == "Not Currently Available")
{
echo "Out of Stock";
}
else
{
echo "<form action='http://www.domain.com/cart.asp' method='post'>
<input type=hidden name=itemcode value='$row->shopcode'>
<input type=hidden name=storeid value='44175'>
<input type=submit value='Add to basket' style='width: 100px'>
</form>";
}
I am using this code now then. I also have the Javascript elsewhere on the page to see what the original output is - which is "Not Currently Available", yet I am getting a result from the query above of the <form>.
Something isn't right....
Re: Can you query a Javascript Query?
Posted: Thu Sep 17, 2009 7:25 am
by simonmlewis
Code: Select all
$contents = file_get_contents("http://www.remoteprice.com/data.asp?storeid=12345&itemcode=$row->romancode&type=4");
if ($contents == "Not Currently Available") {
echo "No";
} else {
echo "Yes";
}
I have also tried this, but all it renders is "Yes" when I no the one I am looking at should render "No".
I have also tried it with {$row->romancode}.
Re: Can you query a Javascript Query?
Posted: Thu Sep 17, 2009 7:30 am
by simonmlewis
This is the output if I echo $contents:
var cText = 'Not Currently Available' document.write(cText)
I need the bit in the middle....

Re: Can you query a Javascript Query?
Posted: Thu Sep 17, 2009 1:22 pm
by requinix
simonmlewis wrote:This is the output if I echo $contents:
var cText = 'Not Currently Available' document.write(cText)
Well that's stupid.
What about
Code: Select all
if ($contents == "var cText = 'Not Currently Available'\ndocument.write(cText)") {
Try \r\n (instead of \n) if that doesn't work.
What's that &type= in the URL for? Is there any way to change the output they give?
Re: Can you query a Javascript Query?
Posted: Thu Sep 17, 2009 2:12 pm
by simonmlewis
Code: Select all
$contents = file_get_contents("http://www.remoteprice.com/data.asp?storeid=12345&itemcode={$row->romancode}&type=4");
if ($contents == "var cText = 'Not Currently Available' \r\ndocument.write(cText)") {
echo "No";
} else {
echo "Yes";
}
echo "$contents<br/>
Still produces Yes even though you can see below what it renders when you echo $contents.
Yes var cText = 'Not Currently Available' document.write(cText)
Not Currently Available
Type is part of their script, not mine.
Re: Can you query a Javascript Query?
Posted: Thu Sep 24, 2009 9:39 am
by simonmlewis
This still isn't working - even though I can render the result, and query on that result from what I have been instructed here, it won't do it.
It's so important that I can get this to work, as I have a system that needs to see if something is in stock or not - if 'not', then it should do something else.
Re: Can you query a Javascript Query?
Posted: Thu Sep 24, 2009 10:51 am
by Li0rE
maybe instead of seeing if the string is identical to what is on the page, you can use the php function strstr to determine whether or not it says that.
if(strstr($contents, 'Not Currently Available'))
{
echo "No";
}
etc...
Re: Can you query a Javascript Query?
Posted: Thu Sep 24, 2009 11:05 am
by simonmlewis
Code: Select all
$contents = file_get_contents("http://www.remoteprice.com/data.asp?storeid=12345&itemcode={$row->code}&type=4");
if(strstr($content, 'Not Currently Available'))
{
echo "no";
}
else
{
echo "yes";
}
I used this. What SHOULD have shown up on the page I looked at was "no" because I had already rendered the javascript result which said on the screen "Not Currently Available".
The strstr produced "yes".
In fact, it produces "yes" whether it is in stock or not in stock.

Re: Can you query a Javascript Query?
Posted: Thu Sep 24, 2009 11:22 am
by Mirge
Re: Can you query a Javascript Query?
Posted: Thu Sep 24, 2009 11:38 am
by simonmlewis
Well this is the actual Javascript in the PHP echoed code that produces the result of "Not Currently Available" when the database is out of stock.
When it's in stock, it just shows nothing.
Code: Select all
<SCRIPT LANGUAGE=\"JavaScript\" SRC=\"http://www.remoteprice.com/data.asp?storeid=44175&itemcode=40703&type=4\"></script>
Re: Can you query a Javascript Query?
Posted: Thu Sep 24, 2009 11:47 am
by Mirge
Thanks. So using:
http://www.remoteprice.com/data.asp?sto ... 703&type=4
Code: Select all
<?php
$url = "http://www.remoteprice.com/data.asp?storeid=44175&itemcode=40703&type=4";
$contents = file_get_contents($url);
if(strpos($contents, "Not Currently Available") !== false) {
print "Not available.";
} else {
print "Available.";
}
?>
The script outputs "Not available." for me... which is what it should be. Re-test with something that IS available to make sure it's correct.
Re: Can you query a Javascript Query?
Posted: Thu Sep 24, 2009 11:58 am
by simonmlewis
You little genius.
It's take a week or two to crack this - and that is EXACTLY it.
Can you please talk me through what:
Code: Select all
if(strpos($contents, "Not Currently Available") !== false)
.. means, in case I need this sort of thing again?

Re: Can you query a Javascript Query?
Posted: Thu Sep 24, 2009 12:06 pm
by Mirge
simonmlewis wrote:You little genius.
It's take a week or two to crack this - and that is EXACTLY it.
Can you please talk me through what:
Code: Select all
if(strpos($contents, "Not Currently Available") !== false)
.. means, in case I need this sort of thing again?

http://www.php.net/strpos/
Essentially, I'm just using strpos() (string position) to determine whether or not the text "Not Currently Available" is in $contents or not. $contents contains the Javascript received from $url.
strpos() returns an index if it finds the text.. of the starting position of the text. If it is NOT found anywhere, it returns false. I used the "identical to" (or rather, NOT identical to) operator to test accurately against false... using != instead of !== could cause problems.
With $foo != false... $foo = 0 is false, $foo = false is false.
With $foo !== false... $foo = 0 is NOT false, $foo = false IS false.
So anyway... we were just checking to see if "Not Currently Available" was in there... if it was, say not available.. if it wasn't.. it's available. Hope it helps.