crimsonmage Posted April 20, 2007 Share Posted April 20, 2007 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 https://forums.phpfreaks.com/topic/47915-php-scraping-javascript/ Share on other sites More sharing options...
neel_basu Posted April 20, 2007 Share Posted April 20, 2007 You can use AJAX to parse the XML doc on page.php?var=val You can also use cookies document.cookies and setcookie() Link to comment https://forums.phpfreaks.com/topic/47915-php-scraping-javascript/#findComment-234164 Share on other sites More sharing options...
taith Posted April 20, 2007 Share Posted April 20, 2007 i agree... set the variable via a cookie... then load a php via ajax, that way php has access to your page and variables, and... you dont have to switch pages Link to comment https://forums.phpfreaks.com/topic/47915-php-scraping-javascript/#findComment-234168 Share on other sites More sharing options...
crimsonmage Posted April 20, 2007 Author Share Posted April 20, 2007 Ok, forgive my PHP ignorance - I havent called PHP in ajax I have done the cookie setting So how would I load a PHP via ajax - a simple code snippit would be fine, i can figure it out from there Thanks so much for everyones help Corey Link to comment https://forums.phpfreaks.com/topic/47915-php-scraping-javascript/#findComment-234209 Share on other sites More sharing options...
taith Posted April 20, 2007 Share Posted April 20, 2007 http://www.phpfreaks.com/forums/index.php/topic,115581.0.html Link to comment https://forums.phpfreaks.com/topic/47915-php-scraping-javascript/#findComment-234215 Share on other sites More sharing options...
neel_basu Posted April 20, 2007 Share Posted April 20, 2007 So how would I load a PHP via ajax - a simple code snippit would be fine, i can figure it out from thereIf you find AJAX Hard you dont need to use AJAX. Simply Load the php script e.g. page.php and in that file use $_SESSION['var_name']; Link to comment https://forums.phpfreaks.com/topic/47915-php-scraping-javascript/#findComment-234221 Share on other sites More sharing options...
crimsonmage Posted April 20, 2007 Author Share Posted April 20, 2007 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 https://forums.phpfreaks.com/topic/47915-php-scraping-javascript/#findComment-234250 Share on other sites More sharing options...
taith Posted April 20, 2007 Share Posted April 20, 2007 bingo... php and javascript cookies are completly interchangeable... Link to comment https://forums.phpfreaks.com/topic/47915-php-scraping-javascript/#findComment-234252 Share on other sites More sharing options...
neel_basu Posted April 21, 2007 Share Posted April 21, 2007 $_SESSION['var_name'];Sorry I was wrong its $_COOKIES SESSION is not accecible by Javascript as its cliant side Link to comment https://forums.phpfreaks.com/topic/47915-php-scraping-javascript/#findComment-234522 Share on other sites More sharing options...
crimsonmage Posted April 21, 2007 Author Share Posted April 21, 2007 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 https://forums.phpfreaks.com/topic/47915-php-scraping-javascript/#findComment-234781 Share on other sites More sharing options...
neel_basu Posted April 21, 2007 Share Posted April 21, 2007 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 https://forums.phpfreaks.com/topic/47915-php-scraping-javascript/#findComment-234793 Share on other sites More sharing options...
crimsonmage Posted April 22, 2007 Author Share Posted April 22, 2007 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 https://forums.phpfreaks.com/topic/47915-php-scraping-javascript/#findComment-235167 Share on other sites More sharing options...
neel_basu Posted April 22, 2007 Share Posted April 22, 2007 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 https://forums.phpfreaks.com/topic/47915-php-scraping-javascript/#findComment-235177 Share on other sites More sharing options...
crimsonmage Posted April 22, 2007 Author Share Posted April 22, 2007 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 https://forums.phpfreaks.com/topic/47915-php-scraping-javascript/#findComment-235207 Share on other sites More sharing options...
neel_basu Posted April 22, 2007 Share Posted April 22, 2007 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 https://forums.phpfreaks.com/topic/47915-php-scraping-javascript/#findComment-235349 Share on other sites More sharing options...
crimsonmage Posted April 22, 2007 Author Share Posted April 22, 2007 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 https://forums.phpfreaks.com/topic/47915-php-scraping-javascript/#findComment-235591 Share on other sites More sharing options...
neel_basu Posted April 23, 2007 Share Posted April 23, 2007 you cannot have the javascript run first prior to php. Because PHP is server code and javascript is browser controlledJavascript 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 https://forums.phpfreaks.com/topic/47915-php-scraping-javascript/#findComment-235756 Share on other sites More sharing options...
crimsonmage Posted April 23, 2007 Author Share Posted April 23, 2007 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 https://forums.phpfreaks.com/topic/47915-php-scraping-javascript/#findComment-236113 Share on other sites More sharing options...
neel_basu Posted April 23, 2007 Share Posted April 23, 2007 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 https://forums.phpfreaks.com/topic/47915-php-scraping-javascript/#findComment-236148 Share on other sites More sharing options...
neel_basu Posted April 23, 2007 Share Posted April 23, 2007 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 https://forums.phpfreaks.com/topic/47915-php-scraping-javascript/#findComment-236153 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.