...

View Full Version : Is there any way to turn this javascript code into a php code?



elitis
12-04-2010, 12:41 AM
This may be confusing but I want to turn
javascript: array=document.getElementsByTagName('a'); for (x in array){ if (array[x].getAttribute('class')=="clickthru-link"){ alert(array[x].href); } } void(0); into a php code. If you don't understand I want to make a php code that does the same thing as that javascript code. I need it as a php code because there are some specific functions that would depend on it to work properly.

mlseim
12-04-2010, 12:57 AM
Describe more what it does ...
PHP can't read specific form fields in real time like JS can.

firepages
12-04-2010, 12:58 AM
PHP can not interact with html at page display, so you could not alert() anything, you could use the DOM functions to parse the page but you wont get to display anything at runtime without javascript ~

elitis
12-04-2010, 01:22 AM
Describe more what it does ...
PHP can't read specific form fields in real time like JS can.
It gets the link of a button (on a site. Not mine though) and displays it.

PHP can not interact with html at page display, so you could not alert() anything, you could use the DOM functions to parse the page but you wont get to display anything at runtime without javascript ~

well not that code exactly but a variation of it which displays the link (Which I know php can do using echo) instead of putting it in an alert box.

mlseim
12-04-2010, 01:42 AM
So the idea is that you want PHP to "scrape" the other site, which means it
scans through their HTML and finds the button you are looking for?

Do you have an example of the "other site", where you're supposed to get the button link?

I'm still not following the meaning or purpose of this.

elitis
12-04-2010, 01:59 AM
So the idea is that you want PHP to "scrape" the other site, which means it
scans through their HTML and finds the button you are looking for?

Do you have an example of the "other site", where you're supposed to get the button link?

I'm still not following the meaning or purpose of this.

yes and no. I want it to locate the button, get the link the button takes you to, AND display the link (and as I said before I know php can do the last part with echo). There are multiple buttons though which I need the links of.

But unfortunately I can't show you the other site because it requires a membership (free) to see the content

elitis
12-04-2010, 06:32 PM
bump.

mlseim
12-04-2010, 07:00 PM
You have a few hurdles to cross.

The first one is to get PHP to log into that other site:
http://www.google.com/search?q=php+log+into+site+and+parse+HTML#sclient=psy&hl=en&q=php+log+into+site+using+CURL&aq=f&aqi=&aql=&oq=&gs_rfai=&pbx=1&fp=e18ec2db23988f7d

Then, you have to read the HTML and parse-out the links.
Without seeing that other site ... nothing I can do to help you with that.

I can tell you, it probably won't be easy.
You might want to find out if the "other site" has some way for you to read a
database or XML file (RSS feed or whatever) to get the information you need.

elitis
12-04-2010, 07:07 PM
You have a few hurdles to cross.

The first one is to get PHP to log into that other site:
http://www.google.com/search?q=php+log+into+site+and+parse+HTML#sclient=psy&hl=en&q=php+log+into+site+using+CURL&aq=f&aqi=&aql=&oq=&gs_rfai=&pbx=1&fp=e18ec2db23988f7d

Then, you have to read the HTML and parse-out the links.
Without seeing that other site ... nothing I can do to help you with that.

I can tell you, it probably won't be easy.
You might want to find out if the "other site" has some way for you to read a
database or XML file (RSS feed or whatever) to get the information you need.

well actually for the login part I was going to have it where you'd have to already be logged into the other site for it to work. The site is http://www.lockerz.com but as I said you have to have an account in order to see anything

mlseim
12-04-2010, 10:09 PM
Your PHP script is not you.

It doesn't matter if you're logged in or not.
The PHP script runs from your webhost's server, and it's not human.
It doesn't use a browser with cookies. You'll have to program the PHP
script login using your personal login account username/password.

That's why I mentioned you find out if the information you need can be
provided by them via an API or RSS feed.

elitis
12-05-2010, 12:34 AM
Your PHP script is not you.

It doesn't matter if you're logged in or not.
The PHP script runs from your webhost's server, and it's not human.
It doesn't use a browser with cookies. You'll have to program the PHP
script login using your personal login account username/password.

That's why I mentioned you find out if the information you need can be
provided by them via an API or RSS feed.
Well I won't be the only one using it. So would a form asking for the user's username and password work? And I don't believe they have an api or rss feed.

Lamped
12-05-2010, 01:02 AM
If they have an RSS feed, you shouldn't need the login details otherwise the API would be the way to go. Failing both, you'd need to do some pretty fancy stuff involving getting the user login via a form, communicating with the server via cURL (with cookies and possibly SSL) and lots of regex or SimpleXML parsing.

mlseim
12-05-2010, 02:05 AM
See post #8.

You have to use PHP CURL POST ... no other way to do it.

You also have to ask users to give you their username and password,
which is probably going to happen on an unsecure server?

I think you are "bumping" this thread in order to get an easy answer from someone.
Nothing I can think of is easy. sorry.

elitis
12-05-2010, 02:45 AM
See post #8.

You have to use PHP CURL POST ... no other way to do it.

You also have to ask users to give you their username and password,
which is probably going to happen on an unsecure server?

I think you are "bumping" this thread in order to get an easy answer from someone.
Nothing I can think of is easy. sorry.

bumped this thread because it was getting close to the 2nd page. True, I'd like there to be an easy answer (who wouldn't, right?) but I expected there wouldn't be an easy way to do this. I have been looking at curl login's and the one I'm using isn't working. It just shows a blank page.


<?php
// INIT CURL
$ch = curl_init();

// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL, 'lockerz.com/auth/login');

// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);

// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'handle=blahblah&password=blahblah');

// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

# Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
# not to print out the results of its query.
# Instead, it will return the results as a string return value
# from curl_exec() instead of the usual true/false.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

// EXECUTE 1st REQUEST (FORM LOGIN)
$store = curl_exec ($ch);

// CLOSE CURL
curl_close ($ch);

return $store;

?>

elitis
12-05-2010, 02:53 AM
also, I just made a test account so you can see exactly what I'm talking about/ trying to do.

Go to lockerz.com and login with the details: email:e.holloway01@yahoo.com and combination (password) is hellboy98

Then go to http://www.lockerz.com/profile/114798 The javascript code will display the links of the decalz (which are products). Hopefully this helps you understand more of what I'm trying to do.

Lamped
12-05-2010, 04:35 AM
You missed out the essential line:

curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);

And you did return $store, not echo $store.

Though using your login details and those extra lines, I think I got some kind of registration page... I think the login needs some extra work, user-agent tweaks, referer setting or something.

elitis
12-06-2010, 11:14 AM
You missed out the essential line:

curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);

And you did return $store, not echo $store.

Though using your login details and those extra lines, I think I got some kind of registration page... I think the login needs some extra work, user-agent tweaks, referer setting or something.

added those lines in but still nothing. It shows the site but still doesn't log in.

Lamped
12-06-2010, 11:52 AM
Yup, the data being passed doesn't appear to be sufficient. This is fast becoming quite a lot of work. You'll need to double-check the data being passed from the browser and compare it to the data being passed with cURL.

elitis
12-06-2010, 09:54 PM
Yup, the data being passed doesn't appear to be sufficient. This is fast becoming quite a lot of work. You'll need to double-check the data being passed from the browser and compare it to the data being passed with cURL.

:confused:

Lamped
12-06-2010, 10:01 PM
k...

Are you sending the right data with cURL?

1. What user-agent are you using?

2. What referer [sic] is set?

elitis
12-07-2010, 11:07 AM
k...

Are you sending the right data with cURL?

1. What user-agent are you using?

2. What referer [sic] is set?

1. No user-agent

2. No sic



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum