openstrife Posted July 26, 2011 Share Posted July 26, 2011 Ok I'll start off on saying, I have experiences with other languages, but I'm fairly new to php. <input type="hidden" autocomplete="off" id="post_form_id" name="post_form_id" value="8eb5c8e454f1497750f930e0a0803115" /><input type=\"hidden\" autocomplete=\"off\" name=\"xhpc_targetid\" value=\"100002643591770\" />input type=\"hidden\" autocomplete=\"off\" name=\"xhpc_composerid\" value=\"u654984_4\" /> Say that's a snippet of code from an HTML page. What I'm trying to do is basically retrieve the value only, and store it in a variable. So Say I have $post_form_id =???; What would I put to retrieve the value from the above code? Usually in one language I used to mess around with.. I could just do something like post_form_id=between(s, 'name="post_form_id" value="', ' /><input type') and post_form_id would = 8eb5c8e454f1497750f930e0a0803115 How would I do this in php? I basically have a form on a page that I want to submit, but it requires like 10 other hidden values on the page to be submitted that change each time the page is refreshed. So I need to be able to retrieve these values and store them to a variable since I am unable to set them statically in the script. Any tips? Many thanks. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 26, 2011 Share Posted July 26, 2011 it's easier than you think in PHP. you basically use the name tag of your element as the php variable name inside a special array that holds all posted items called $_POST. (I know, sounds confusing like that) for this: <input type="hidden" autocomplete="off" id="post_form_id" name="post_form_id" value="8eb5c8e454f1497750f930e0a0803115" /> you would grab the value with: $_POST['post_form_id']; so to store the value in another variable, just use: $var = $_POST['post_form_id']; hope this helps Quote Link to comment Share on other sites More sharing options...
openstrife Posted July 26, 2011 Author Share Posted July 26, 2011 Do I need to be on the page? I am using cURL to log into facebook, which it does succesfully, but then what? I've got this. <?PHP $email = "email"; $password = "pass"; $ch = curl_init(); $poststring = "email=" . $email . "&pass=" . $password . "&login=Login"; curl_setopt($ch, CURLOPT_URL, 'https://www.facebook.com/login.php?login_attempt=1;next=http://facebook.com/home.php'); curl_setopt($ch, CURLOPT_POSTFIELDS,"$poststring"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt"); curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3"); echo $_POST['post_form_id']; //<-- This gives me an error " Notice: Undefined index: post_form_id" curl_exec($ch); echo $_POST['post_form_id']; //<-- This gives me an error " Notice: Undefined index: post_form_id" Also, why do I need to keep entering a verification? I plan to be very frequent on these forums. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 26, 2011 Share Posted July 26, 2011 Also, why do I need to keep entering a verification? I plan to be very frequent on these forums. verification should go away after a couple more posts. $_POST is to retrieve values sent through a form using method="post". CURL has nothing to do with this. in your case, the resulting response can be grabbed with $response = curl_exec($ch); (instead of your last line) Quote Link to comment Share on other sites More sharing options...
openstrife Posted July 26, 2011 Author Share Posted July 26, 2011 So... I misunderstand what you're saying? I am retrieving values that are in the current pages html, that are going to be posted to a form. What is the purpose of the $response in terms of retrieving the values of data that is going to be posted? Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 26, 2011 Share Posted July 26, 2011 it's one thing to post an html form (i.e. reloading the page or jumping to another page) and it's another to post variables using CURL (page does not refresh). if you're using the first option (html form posted) then your variables will be in $_POST Quote Link to comment Share on other sites More sharing options...
openstrife Posted July 26, 2011 Author Share Posted July 26, 2011 I don't really care about the page refreshing. I still need the variables off the current page though, to post TO a url. I don't care if it gets opened or not. Like I am trying to automate status posting. I just need to send the post data to the .php file that causes my status to update. I don't care if the page refreshes. I'm basically trying to use the same method I used to login, but to post different variables to a different url. Just a number of those variables are located on the page that my browser arives on when I use the script(the facebook home page). How do I copy the variables that I want to use off that page? What I have now succesfully lands me on and displays the facebook homepage, but from there I am lost. . I appreciate your help a lot. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 26, 2011 Share Posted July 26, 2011 Ah! I think I finally understand what you need... let me say this first: PHP is a server-side scripting language, which basically means that all php code executes before your page has loaded. from what I understand, you land on a page that has those form elements and you would like to retrieve their values to send somewhere else. Since php has already executed at this point, it will be of little help. you need to use javascript to grab the values, and ajax to send them off to another php code. Quote Link to comment Share on other sites More sharing options...
openstrife Posted July 26, 2011 Author Share Posted July 26, 2011 Hmm sounds complex but I guess that's because I don't know how to do that. but I tried to do the same method I used with logging in, with posting a status and typing in each individual variable... but it didn't work. It makes me think my cookies are not working correctly but I do not know a method to check if they are or not. Do you have any tutorial/guide link, or even an explanation on how to use javascript to grab them and use agax to send them to another code? Would this require a separate php file? Ultimately, my goal was to make a php script that can log into facebook, and then basically monitor the system time and initiate specific status post at predetermined times that I set up in the script. What kind of setup would I be looking at for this? A combination of javascript with multiple php scripts? Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 26, 2011 Share Posted July 26, 2011 since a loaded page has already executed all the php code, there are only 2 ways you can make it do something else with php: 1. jump to another page or refresh 2. use ajax (fancy way of calling other php scripts on the server without reloading the page) It's hard to give you a complete solution since you never really discussed what variables you're talking about. in your first post I got to understanding you basically had an html form with values you needed to grab when submitted, but this is not the case. my question is: what variables are we talking about exactly? things returned from the facebook login script? variables that show up in hidden forms on your page, placed there by the facebook login script? or stuff you actually had initially and you just want them to be available all other the place (in this case you need $_SESSION to store them). Quote Link to comment Share on other sites More sharing options...
openstrife Posted July 26, 2011 Author Share Posted July 26, 2011 since a loaded page has already executed all the php code, there are only 2 ways you can make it do something else with php: 1. jump to another page or refresh 2. use ajax (fancy way of calling other php scripts on the server without reloading the page) It's hard to give you a complete solution since you never really discussed what variables you're talking about. in your first post I got to understanding you basically had an html form with values you needed to grab when submitted, but this is not the case. my question is: what variables are we talking about exactly? things returned from the facebook login script? variables that show up in hidden forms on your page, placed there by the facebook login script? or stuff you actually had initially and you just want them to be available all other the place (in this case you need $_SESSION to store them). That what I bolded ^ and an additional variable that I have preset - the status I wish to say. To be even more specific. Here's a pre-gathered list of all the variables that are submitted when I send a status. It's essentially the first 3 in the list below that I wish to grab from the page that facebook's login.php lands me on. I need to grab those 3 each time I want to post a status. Would I need different php scripts per status post? Or? $post_form_id= "8eb5c8e454f1497750f930e0a0803115"; $fb_dtsg = "AQAF0owf"; $xhpc_composerid= "u564889_4"; $xhpc_targetid = "100002643591770"; //This changes based on who's logged in //Basically it's everything above this line changes each time I visit the facebook page where I am able to post a status. They're loaded by the login.php script that facebook uses which lands me on my home page. $xhpc_context= "home"; $xhpc_fbx= ""; $xhpc_timeline= ""; $xhpc_ismeta = ""; $xhpc_message = "What's on your mind?"; //This is the only one I change $UIPrivacyWidget[0]= "80"; //The 4 below usually static unless I say otherwise $privacy_data[value] = "80"; $privacy_data[friends]= "0"; $xhpc_message[list_anon]= "0"; $privacy_data[list_x_anon]= "0"; Also, not sure if it matters but I post the status to https://www.facebook.com/ajax/updatestatus.php , which I can see uses an ajax Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 26, 2011 Share Posted July 26, 2011 well, I'm guessing the return from facebook is being returned in an iframe on your page (is this correct?)... you can try javascript just to test first... going back to you very first post, where you had a hidden element called 'post_form_id'... try grabbing that value with javascript (in your html's <head> section): <script> function getValue(){ var value1 = document.getElementById("post_form_id").value; alert(value1); } </script> and place a link somehwere on your page to call the javascript function after the FB login has completed... <a href="#" onclick="getValue();">clickMe</a> test this situation to see if JS can grab your variables properly (it works, I just don't know if it will work with an iFrame) and post the results so we can brainstorm a bit more. Quote Link to comment Share on other sites More sharing options...
openstrife Posted July 26, 2011 Author Share Posted July 26, 2011 Yep it retrieved the value successfully. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 26, 2011 Share Posted July 26, 2011 ok, cool... so now what? from what I understood, you need to perform certain actions at given intervals of time? we know we can grab the variables, now we need to know exactly when and what you would like to do with them. Quote Link to comment Share on other sites More sharing options...
openstrife Posted July 26, 2011 Author Share Posted July 26, 2011 Well, if I knew php throughly enough, my next step with any scripting language... would be to make it take the values it retrieves and put them through another php script and actually be able to post a status on facebook. Worry about the timer after I get it to actually post a status Even when I preset all of those variables it was still unable to post a status and I suspect this is because the cookies aren't working... it gives me no feed back or anything and I really have no way to check if it's even still logged in when it sends those variables to https://www.facebook.com/ajax/updatestatus.php.. I just no, no status shows up. What I did ultimately was below the login code I just posted. $post_form_id= "8eb5c8e454f1497750f930e0a0803115"; $fb_dtsg = "AQAF0owf"; $xhpc_composerid= "u564889_4"; $xhpc_targetid = "100002643591770"; $xhpc_context= "home"; $xhpc_fbx= ""; $xhpc_timeline= ""; $xhpc_ismeta = ""; $xhpc_message = "What's on your mind?"; $UIPrivacyWidget[0]= "80"; $privacy_data[value] = "80"; $privacy_data[friends]= "0"; $xhpc_message[list_anon]= "0"; $privacy_data[list_x_anon]= "0"; $ch = curl_init(); $poststring = "post_form_id=" . $post_form_id . "&fb_dtsg=" . $fb_dtsg . "&xhpc_composerid=" . $xhpc_composerid . "&xhpc_targetid=" . $xhpc_targetid . "&xhpc_context=" . $xhpc_context . "&xhpc_fbx=" . $xhpc_fbx. "&xhpc_timeline=" . $xhpc_timeline. "&xhpc_ismeta=" . $xhpc_ismeta. "&xhpc_message=" . $xhpc_message. "&UIPrivacyWidget[0]=" . $UIPrivacyWidget[0]. "&privacy_data=" . $privacy_data. "&privacy_data[friends]=" . $privacy_data[friends]. "&xhpc_message[list_anon]=" . $xhpc_message. "&privacy_data[list_x_anon]=" . $UIPrivacyWidget[0]; curl_setopt($ch, CURLOPT_URL, 'https://www.facebook.com/ajax/updatestatus.php;next=http://facebook.com/home.php'); curl_setopt($ch, CURLOPT_POSTFIELDS,"$poststring"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt"); curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt"); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3"); curl_exec($ch); curl_close($ch); unset($ch); But that didn't work. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 26, 2011 Share Posted July 26, 2011 there's a lot of info on how to update status messages, maybe start here: https://developers.facebook.com/docs/reference/rest/stream.publish/ Quote Link to comment Share on other sites More sharing options...
openstrife Posted July 26, 2011 Author Share Posted July 26, 2011 That's for applications though isn't it?... and it will say on my page that I posted from an application I believe, which I can't have... though I don't know how to put any of the stuff to use on this page :X. I'm not asking you to make my script for me by any means... just i'm looking for a general idea of how I would take the values that javascript retrieves and utilize them in another script? Would this script need to be in another file? How would I use agax to call it and transfer those variables? What I'm thinking right now... is that I would need a separate php script for posting a status? Do the first timer in javascript in the same page that I logged in with, through that use Agax to transfer variables and use another script to post a status... and the timer could still be going on the main page waiting until a certain time to tell it to post the next status? Idk.. how would I do this if I want to post 12-13 predetermined statuses each at it's own time? Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 26, 2011 Share Posted July 26, 2011 add something like this in your head section (it's another javascript function) Change the filename variable and the params and call it from a link in your html code just for testing purposes (like you called the previous one). and create a php file that just echo's something to test. function callPhpFile(){ var url = 'phpFileName.php'; var params = '&valid=1'; // add all variables you need to pass along here var xmlhttp = false; // check browser and create request if(window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); if(xmlhttp.overrideMimeType){ xmlhttp.overrideMimeType('text/xml'); } } else if(window.ActiveXObject){ try{ xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch(e){ try{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ } } } if(!xmlhttp) { return false; } xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState==4){ // assuming the php file echo's something, it will be returned here var result = xmlhttp.responseText; if(result != '') alert(result); } } // POST STUFF xmlhttp.open("POST", url, true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.setRequestHeader("Content-length", params.length); xmlhttp.setRequestHeader("Connection", "close"); xmlhttp.send(params); } Quote Link to comment Share on other sites More sharing options...
xyph Posted July 26, 2011 Share Posted July 26, 2011 You just want a form that logs into Facebook and updates a status? Correct? I can't help you with this, I don't have/want a Facebook account, but I can try to clarify what you want. Quote Link to comment Share on other sites More sharing options...
openstrife Posted July 27, 2011 Author Share Posted July 27, 2011 Okay with that above function is was succesfully able to call another php script... but what's the point in that when it appears to do a post at the bottom of the function to the url? I don't really know syntax in javascript... so how would combine variables into another variable? var params = '&valid=1'; var post_form_id = document.getElementById("post_form_id").value; var fb_dtsg = document.getElementById("fb_dtsg ").value; var xhpc_composerid = document.getElementById("xhpc_composerid").value; Er how do I add those 3 variables into the string up there? I know it's simple, but it's syntax I don't know Er sorry if I'm confusing on explaining that... Quote Link to comment Share on other sites More sharing options...
openstrife Posted July 27, 2011 Author Share Posted July 27, 2011 Do the params that I send to the other php script, do they land as php variables that are ready to use? How do I put them to use in that second php script? Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 27, 2011 Share Posted July 27, 2011 just add the strings and values usinf the plus sign to concatenate: var post_form_id = document.getElementById("post_form_id").value; var fb_dtsg = document.getElementById("fb_dtsg ").value; var xhpc_composerid = document.getElementById("xhpc_composerid").value; var params = '&post_form_id=' + post_form_id + '&fb_dtsg=' + fb_dtsg + '&xhpc_composerid='+xhpc_composerid; that javascript function posts these values to your php file, so in that file you will grab these values with: $var1 = $_POST['xhpc_composerid']; Quote Link to comment Share on other sites More sharing options...
openstrife Posted July 27, 2011 Author Share Posted July 27, 2011 Didn't know how to describe the error I got... so I took a SS(attached) [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 27, 2011 Share Posted July 27, 2011 variables names in $_POST are enclosed in quotes (double or single).. so $_POST['something']; // is valid $_POST["something"]; // is valid $_POST[something]; // NOT valid Quote Link to comment Share on other sites More sharing options...
openstrife Posted July 27, 2011 Author Share Posted July 27, 2011 That worked :}. Will I be able to post data to the logged in account from the previous click on the new .php link? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.