Jump to content

Php Scraping Javascript


crimsonmage

Recommended Posts

Ok, I have been pulling my hair out - and I am basically about to accept that this cannot be done, but I am trying to scrape a page that I created so that I can get the javascript variable into PHP.

 

Here is the link http://www.multiplottr.com/getLatLong.html?address=washington,%20dc

 

and at the bottom is the code to that page.

 

From what I understand - curl cannot process javascript. I know that there are a ton of ways of sending a javascript variable to PHP - but it has to be done by a screen refresh - I cannot do that because I have a bunch of other things going on on that page - so HOW could I get the value of the javascript I am needing without a refresh - if there is no way to scrape javascript.

 

The below example uses the dom script to place the value into a div- but when I use curl, the javascript doesnt run - so I dont get the value that I need

 

Any suggestions?

 

Thanks

 

Crimson

--------------------------------------------------------

 

Here is the code to the link i gave above:

 

<script language="javascript">

 

function gup( name )

{

  var regexS = "[\\?&]"+name+"=([^&]*)";

  var regex = new RegExp( regexS );

  var tmpURL = window.location.href;

  var results = regex.exec( tmpURL );

  if( results == null )

    return "";

  else

    return results[1];

}

 

var localSearch = new GlocalSearch();

var resultLat;

var resultLng;

var carrythisAddress=gup( 'address' );

carrythisAddress = unescape(carrythisAddress);

 

 

 

function usePointFromPostcode(carrythisAddress) {

 

localSearch.setSearchCompleteCallback(null,

function() {

     

if (localSearch.results[0]) {   

resultLat = localSearch.results[0].lat;

            resultLng = localSearch.results[0].lng;

document.getElementById('findCoordinates').innerHTML = resultLat+', '+resultLng;

}

else{

        document.getElementById('findCoordinates').innerHTML = "Postcode not found!";

        }

    });     

localSearch.execute(carrythisAddress);

}

 

 

 

usePointFromPostcode(carrythisAddress);

 

</script>

</head>

<body>

<div id="findCoordinates"></div>

</body>

</html>

Link to comment
Share on other sites

So let me see if I have this right

 

I set my javascript variable to a cookie - that part is easy

I then load a php file - either using ajax or load a php file - (page.php)

that will then load the cookie i set using javascript, and then i can read it using php because loading that file also acquired the set cookie?

 

Thanks

 

Corey

 

 

Link to comment
Share on other sites

alright, I am not getting this

 

I have the javascript writing the cookie -

 

Now what would be the script that I could use to pull the data using PHP - I tried using an IFRAME to load the info - but that didnt work.

 

This is what I did:

 

I have javascript writing to document.cookie - it works, because i can see the results in an alert- but getting it into PHP is pure HELL.

 

I looked at the ajax link you gave, but it doesnt say anything about loading a php file inside a php file

 

Thanks for everything, when this is done - I will give the solution, so those that want the same thing can find it

 

Thanks

 

Corey

Link to comment
Share on other sites

AJAX is not needed as I told above. but for advanced you may use it.

------------------------------------------------------------------

Simply Load the php file (page1.php) to your browser AFTER setting the Cookies with Javascript.

And in that page1.php file Use print_r($_COOKIES);

Link to comment
Share on other sites

Ok, it would be wonderful if this worked - but it doesnt

 

here is the link http://www.multiplottr.com/getLatLong.php

 

when you initially load this page, it doesnt work - the reason why - is that php runs the whole page before running the javascript - 

 

Here is the code

<?php

echo "<script src='http://www.google.com/uds/api?file=uds.js&v=1.0&key=ABQIAAAAAM0T_bVuwih_zXGOB7yP9xQ-AcVvwliuEilMpsg5Wfr8-Pb50hQL0mNmncRFsRhSm2NKZWWAToNbyg' type='text/javascript'></script>\n";

echo "<script language='javascript'>\n";

echo "

var localSearch = new GlocalSearch();

var resultLat;

var resultLng;

carrythisAddress = 'Washington, DC';\n

 

usePointFromPostcode(carrythisAddress);

 

function usePointFromPostcode(carrythisAddress) {

 

localSearch.setSearchCompleteCallback(null,

function() {

     

if (localSearch.results[0]) {   

resultLat = localSearch.results[0].lat;

            resultLng = localSearch.results[0].lng;

document.cookie = 'latitude='+resultLat+';';

document.cookie = 'longitude='+resultLng+';';

alert(document.cookie);

}

else{

        alert('Postcode not found!');

        }

 

 

    });     

localSearch.execute(carrythisAddress);

}\n

";

 

echo "</script>\n";

echo "<iframe src='getAddress.php' width='300' height='300'></iframe>";

$resultsLat = $HTTP_COOKIE_VARS["latitude"];

$resultsLng = $HTTP_COOKIE_VARS["longitude"];

echo "<br>Coordinates are ".$resultsLat.", ".$resultsLng;

?>

 

You will notice that I call the Iframe AFTER the javascript, but it still doesnt work - it runs all of the PHP before running the javascript - and thats where the problem is

 

If you refresh, then it works fine

 

So the question is - How do we run the javascript before it runs the rest of the code?

 

Thanks

 

Corey

Link to comment
Share on other sites

Yes First Javascript is setting the cookie and after that php will access it. here php script and the Javasctipt is loading at the same time. so after 1 refresh Javascripts compleates its Set cookie Job as done Previously.and then Php Gets the cookie.

You Can use these Functions to set and get Cookies with Javascript.

function setcookie(name,value,duration)
{
cookiestring=name+"="+escape(value)+";EXPIRES="+getexpirydate(duration);
document.cookie=cookiestring;
}

function getexpirydate( nodays)
{
var UTCstring;
Today = new Date();
nomilli=Date.parse(Today);
Today.setTime(nomilli+nodays*24*60*60*1000);
UTCstring = Today.toUTCString();
return UTCstring;
}

function getcookie(cookiename)
{
var cookiestring=""+document.cookie;
var index1=cookiestring.indexOf(cookiename);
if (index1==-1 || cookiename=="") return ""; 
var index2=cookiestring.indexOf(';',index1);
if (index2==-1) index2=cookiestring.length; 
return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}

Link to comment
Share on other sites

neel_basu, Thank you so much for taking the time to respond. But the problem isnt being answered

 

And it looks like that the task that was put forward cannot be done.

 

What needs to be done, is have the PHP code pull a variable from the javascript - Since PHP has to run first and then javascript, you cannot get variables from the javascript while the php is running its task.

 

Php is server code - Javascript is client/browser code - PHP will always run first before the Javascript - Now you might be thinking that yes you can get information from the javascript code, and that is probably true once the whole page is loaded. And acquire that info could be handled from some sort of action - such as an onClick or something like that - but you cannot get the information at the same time.

 

Challenge: Prove me wrong - try to get the above code to work. I doubt it can be done, The ultimate goal - the initial page has to be able to display the javascript variable as a php variable.

 

Good Luck, and if you can work it out, you have changed the world!

 

Corey

Link to comment
Share on other sites

Look First set the Cookie with javascript or php

and then get the value of that cookie var with Javascript or php.

Its Simple

1 thing to remember.

Php sets Cookie at the very begening of loading your page as it sends headers to your browser to set the cookie

and Javascript can set cookies in runtime when the page has been loaded.

And you should try to get the cookie value after it has been set How You can get the value of cookie if there is no cookie set.

Its something like echo x; where there is no variable named x.

Link to comment
Share on other sites

Your right, its simple if the page refreshes - its impossible if you need to pull the variables on the same page at the same time - you cannot have the javascript run first prior to php. Because PHP is server code and javascript is browser controlled

 

Everything your saying is theory - I have shown you the code of exactly everything you said - but it cant be done.

 

Again, thank you for your time, but I'll come up with an alternative

 

If anyone figures it out, let me know.

 

Corey

Link to comment
Share on other sites

you cannot have the javascript run first prior to php. Because PHP is server code and javascript is browser controlled
Javascript Can also Run first.

page1.html(Contains Javascript)

The above Cookie functions are in kookefunc.js file

<script src = "kookefunc.js"></script>
<script>
//Seting and getting functions are given above in above js File
setcookie(name,value,duration)//Set the Cookie in this Way
</script>
<!-- Redirect User to a php script -->
<meta http-equiv="refresh" content="2; url=http://localhost/phppage.php" /> 

phppage.php

<?php
print_r($_COOKIES);
?>

Link to comment
Share on other sites

Ah, but thats the rub, I have to call the javascript inside the php - what i gave you was a sampling of a larger application - the whole application is in php - and i am trying to pull a small part of javascript inside.

 

but thank you for all your comments

 

Corey

Link to comment
Share on other sites

Ah, but thats the rub, I have to call the javascript inside the php - what i gave you was a sampling of a larger application - the whole application is in php - and i am trying to pull a small part of javascript inside.

 

but thank you for all your comments

 

Corey

Thats IMPOSSIBLE.Simply impossible. YouCant Call any Javascript function or class inside PHP.

-----------

Untill the page loads (in)compleately to the browser theere is NO Javascript.

And

Onecs the page is compleately loaded to the browser there is no story of php

**I am Talking about Javascript and PHP not AJAX (NOTE : AJAX is not Javascript)

Link to comment
Share on other sites

If you are echo ing Javascript codes to the browser through PHP. then First PHP echo is being executed and sending the Javascript codes as text to the browser. So If you use PHP set cookie Before echo ing Javascript codes. The cookies ar being set first. and after that Its sending the javascript to the browser and when the Javascript(Page) is compleately loads into the browser Javascript Can Find the Cookies AS the Cookies has been sent before Javascript loading.

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.