Hi there! I tried to write a script which can login to my myspace account via php using the snoopy php class for form submission. But the login fails ... I guess it has something to do with cookies which I can not accept via my php script. My script works with several other websites which don't require or use cookies for the login action ... Maybe somebody can give me a tip or tell me how I can login to my myspace account using a php script?! Thanks in advance
Here's what I just recently finished today. I run a train on MySpace and I needed a way for my riders to submit their "proofs" (bulletin URLs), have a PHP script check them, and automatically update a roster so that people on the train appear according to the number of times they've posted the train. Grabbing the page contents of bulletin URLs seemed easy, but actually requires an active login on MySpace's homepage in order to view them, so below is a portion of the code I use that deals specifically with logging into myspace and fetching the contents of a specified page. Anyone can obviously edit it as needed for their own purposes. Notes: I created a blank file, cookie.txt before I began using this script and CHMODed it to 777. <?php //PHP5 //Purpose: Sign in to MySpace with specified account information. //Retreive page contents from a specified page that would otherwise //be unaccessible without a valid login first. $file = fopen("cookie.txt", "w+"); fclose($file); //Flush contents from cookie file before beginning. $url1 = "http://collect.myspace.com/index.cfm?fuseaction=login.process"; //Set MySpace's main login form action. $url2 = "http://home.myspace.com/index.cfm?fuseaction=user"; //MySpace page redirected to after login. $postvars = "email=user%40domain.ext&password=something"; //Set your email and password for your MySpace account. $contentlength = strlen($postvars); //Determine length for content-length header info. $page = "http://..."; //The page on MySpace you'd like to grab from once you're signed in. $agent=$_SERVER["HTTP_USER_AGENT"]; //User-agent as to be seen by MySpace server. //Headers. This script uses two cURL sessions over two separate pages //solely for logging in, mimicing the redirect MySpace performs when you //login. We need to specify separate headers for each page which I've done. $header_myspace_collect = array( "Host: collect.myspace.com", $agent, "Content-Type: application/x-www-form-urlencoded", "Content-Length: " . $contentlength); $header_myspace_home = array( "Host: home.myspace.com", $agent); //URL 1: MySpace Main Login $ch = curl_init(); //Initiate cURL session. curl_setopt($ch, CURLOPT_URL, $url1); //Set URL to POST to. curl_setopt($ch, CURLOPT_HEADER, 1); //Allow headers to be returned. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); //Don't follow redirects. We'll change pages ourself. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Return page data instead of printing it. curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); //Cookie file to save cookies to. curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); //Cookie file MySpace will seek for cookies. curl_setopt($ch, CURLOPT_HTTPHEADER, $header_myspace_collect); //Header to use. curl_setopt($ch, CURLOPT_POST, 1); //Allow HTTP POST. curl_setopt($ch, CURLOPT_USERAGENT, $agent); //User-Agent to be seen by server. curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars); //Allocate fields to POST. $myspace_collect_return = curl_exec($ch); //Execute this cURL session, allocating contents of page. //URL 2: User's MySpace homepage. Necessary to connect here so the //MySpace server acknowledges cURL's request to continue authorizing //your login. curl_setopt($ch, CURLOPT_URL, $url2); //Set URL. curl_setopt($ch, CURLOPT_HEADER, 1); //Allow headers. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); //Don't redirect us. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Return page, don't print it. curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); //Use cookie file for cookies. curl_setopt($ch, CURLOPT_HTTPHEADER, $header_myspace_home); //Use header. curl_setopt($ch, CURLOPT_USERAGENT, $agent); //Use agent. $myspace_home_return = curl_exec($ch); //Execute cURL session and return data to variable. //URL3: Specified URL of your choice. //In my case it was a user-submitted bulletin URL. curl_setopt($ch, CURLOPT_URL, $page); //Same basic routine. curl_setopt($ch, CURLOPT_HEADER, 1); //Header. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); //No redirect. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Return, don't print. curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); //Use file for cookies. curl_setopt($ch, CURLOPT_HTTPHEADER, $header_myspace_home); //Header. curl_setopt($ch, CURLOPT_USERAGENT, $agent); //Agent. $pagecontents = curl_exec($ch); //Execute cURL session, and place page conents into variable. curl_close($ch); //Close cURL session. $file = fopen("cookie.txt", "w+"); fclose($file); //Dump cookies again for security. //Process complete. MySpace page $page sucessfully //stored into $pagecontents. //Now, you can do whatever you want with it. Parse it. //Extract something from it. //Use a regular expression on it. //Etc. ?> Code (markup):
Can I use this to update my MySpace layout from my site. Is there an example of a PHP script that I can use?
Yes, you can use cURL to update your myspace from another site. I don't think there is any freely available right now, but I will be altering one of my scripts so it can do this sometime this week.
Hello - I tried the example posted by yugu above and it doesn't seem to work anymore. Has MySpace taken steps to block dynamic logins to their site? Does anyone have a script that is working currently? Thanks! - Adam
I installed this script and attempted to use it. The result I am getting when I print $pagecontents is: HTTP/1.1 400 Bad Request Content-Type: text/html Date: Tue, 09 Jan 2007 20:31:03 GMT nnCoection: close Content-Length: 42 Bad Request (Invalid Header Name)