Can you query a Javascript Query?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Can you query a Javascript Query?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can you query a Javascript Query?

Post 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
}
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you query a Javascript Query?

Post 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....
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you query a Javascript Query?

Post 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}.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you query a Javascript Query?

Post 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.... :cry:
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can you query a Javascript Query?

Post 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?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you query a Javascript Query?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you query a Javascript Query?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Li0rE
Forum Commoner
Posts: 41
Joined: Wed Jun 07, 2006 6:26 am

Re: Can you query a Javascript Query?

Post 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...
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you query a Javascript Query?

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

:(
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Can you query a Javascript Query?

Post by Mirge »

Give an example URL... like http://www.domain.com/data.asp?storeid= ... de}&type=4 .. but a real URL.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you query a Javascript Query?

Post 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>
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Can you query a Javascript Query?

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you query a Javascript Query?

Post 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?

:drunk:
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Can you query a Javascript Query?

Post 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?

:drunk:
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.
Post Reply