Jump to content

Php + Xml(?)


thescientist

Recommended Posts

Ok, so for a project I'm working on (a Flickr API app), I'm using a program called AVE (it's used in the digital tv applications development field), I want to be able to send an email to a user but I'm limited in that the user can't access the web from the program. However, I am able to use XMLHttp (similar to XMLHTTPRequest) to make calls to a server. So with that in mind, this is what I would like to do.1) From my program: Send an HTTP request with a person's email address and a URL as arguments(***where I need some advice***)2) If I can figure out Step 1, I want to be able to pass the email address to a php page with a mailto() in it, so that the persons email address can become the 'To:' part of the email, and the URL can become the body of the message.3) Have my contact.php send an email to that person with the URL in the bodyIn other words, have my program send the address and the URL (in XML?) to a contact.php page on my server, have the form work by itself and send an email. Can PHP do this by itself? Sorry if this sounds confusing, but basically can a php page work by itself without someone actually having to click a submit button?

Link to comment
Share on other sites

what makes the .php run without having a submit button hit though?
JavaScript's XMLHttpRequest object (and the send() method to be more precise)?Using XMLHttpRequest() is just as sending a request in a browser (the "XML" part is misleading... forget about it). You create an HTTP request, send the request, and get a response.In this case (and any time you send a request to a PHP file), the actual sending of a request is going to trigger PHP, since that is what the web server will call upon receiving the request.Just keep in mind one thing - If you need to process the response for whatever purpose (e.g. reporting errors), set the onreadystatechange event before sending the request, and it it, check if the readyState is 4, and if it is, do what you want with the response at that moment.
Link to comment
Share on other sites

Ok, so if I have this makeshift contact form to accept an email and text (for the url), just making an HTTP request which passes the arguments 'emailaddress' and 'URL' will make it work? Do I have to make anything match so the arguments being sent get passed to the correct form elements, and eventually submitted to my email address?I recognize I am fairly new to this, so thanks for all the help and patience.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>	<title>Flickr API Contact Form</title>	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>	<!--<link rel="stylesheet" type="text/css" href="style.css"/>--></head><body><?php//Start of Checking for Form Validationif (isset($_POST['Submit'])){  	  //Form Validation	  if (!empty($_POST['URL'])&&	   !empty($_POST['EmailAddress']))	  {  	  //To	  $to = 'obuckley@kenzanmedia.com';  	  //Subject	  $subject = 'Flickr Authentication Request';  	  //Create The Body	  $body = 		  "Your Authentication URL		  {$_POST['URL']} \n";   	  //set the siz of the body 	  $body = wordwrap($body, 100);  	  //Send the Email 	  mail($to, $subject, $body);	  //Thank You Message	  echo '<center><font color="#343356">We will respond as soon as we can.  Thanks, and have a nice day!</font></center>';	  //Clear $_POST	  $_POST = array();  	  }  	  else	  {		  echo '<center><font color="#343356">Sorry, there was a blank field in your submition.  Please fill out the required (**) form items along with your request.</font></center>';	  }}?>		<form id="inforequest" method="post" action="index.php">		<h3>Email   **</h3>		<input type="text" name="EmailAddress"  size="17" value="<?php if		(isset($_POST['EmailAddress'])) echo $_POST['EmailAddress']; ?>"/>		<br/>		<h3>URL   **</h3>		<textarea name="URL" cols="60" rows="8"><?php if		(isset($_POST['URL'])) echo $_POST['URL']; ?></textarea>		<br/>		<br/>		<input type="submit" name="submit" value="Submit"/><input name="Reset" type="reset" id="Reset" value="Reset"/>		<input type="hidden" name="Submit" value="email_contact"/>		</div>		  </form> 		<br/>		<br/></body></html>

Link to comment
Share on other sites

what makes the .php run without having a submit button hit though?
A submit button has nothing to do with PHP, all PHP does is respond to requests. It doesn't matter how the requests come in (that is, not unless you're specifically checking for that).
just making an HTTP request which passes the arguments 'emailaddress' and 'URL' will make it work?
Create a request with the appropriate method (get or post), and send your data with the correct names. Make sure to also URL-encode the data if you're creating the request manually, or check to see if the library you're using will do that automatically.The PHP page you're sending the request to is not an HTML page, it does not have a head section or body or form or any other HTML elements on it, it should just get the data from either get or post and process it. You can validate the data and output an error message if it fails validation, and your HTTP request library can get the response from PHP and check for an error message to display to the user. This is the same way ajax works.
Link to comment
Share on other sites

A submit button has nothing to do with PHP, all PHP does is respond to requests. It doesn't matter how the requests come in (that is, not unless you're specifically checking for that).Create a request with the appropriate method (get or post), and send your data with the correct names. Make sure to also URL-encode the data if you're creating the request manually, or check to see if the library you're using will do that automatically.The PHP page you're sending the request to is not an HTML page, it does not have a head section or body or form or any other HTML elements on it, it should just get the data from either get or post and process it. You can validate the data and output an error message if it fails validation, and your HTTP request library can get the response from PHP and check for an error message to display to the user. This is the same way ajax works.
so given my code above, I should omit everything but the php code that actually sends the email?Something like this, then?
<?php//Start of Checking for Form Validationif (isset($_POST['Submit'])){  	  //Form Validation	  if (!empty($_POST['URL'])&&	   !empty($_POST['EmailAddress']))	  {  	  //To	  $to = {$_POST['EmailAddress']};  	  //Subject	  $subject = 'Flickr Authentication Request';  	  //Create The Body	  $body = 		  "Your Authentication URL		  {$_POST['URL']} \n";   	  //set the siz of the body 	  $body = wordwrap($body, 100);  	  //Send the Email 	  mail($to, $subject, $body);	  //Thank You Message	  echo '<center><font color="#343356">We will respond as soon as we can.  Thanks, and have a nice day!</font></center>';	  //Clear $_POST	  $_POST = array();  	  }  	  else	  {		  echo '<center><font color="#343356">Sorry, there was a blank field in your submition.  Please fill out the required (**) form items along with your request.</font></center>';	  }}?>

btw, thanks for everyone's help, its slowly starting to sink in, :)

Link to comment
Share on other sites

More or less, you can remove the first if statement because you can just assume that data was submitted if the page got loaded. For the message you output, it may be better to leave off the HTML markup and handle formatting on the client. Even better, for success you can just output "success", or else you output the error message. On the client, you can check if the response was "success" and show your success message, or else just show the response message.

Link to comment
Share on other sites

More or less, you can remove the first if statement because you can just assume that data was submitted if the page got loaded. For the message you output, it may be better to leave off the HTML markup and handle formatting on the client. Even better, for success you can just output "success", or else you output the error message. On the client, you can check if the response was "success" and show your success message, or else just show the response message.
is there a way in PHP to output success or failure with some sort of XML markup that I could read with my app?
Link to comment
Share on other sites

Create a request with the appropriate method (get or post), and send your data with the correct names. Make sure to also URL-encode the data if you're creating the request manually, or check to see if the library you're using will do that automatically.
could you elaborate on that part?
Link to comment
Share on other sites

also, I'm not sure if you would know this based on the program I am using, but would this look like an appropriate call to my PHP page?g_httpConn.open("post", httpServerURL , false); //third argument is about whether the call is done asynchronous or not or is sending a page with like that completely wrong?edit: this is the other method i've been trying, which I think is the way it should be done. (although still won't work)

scene.Button2.onClick = function () {	var httpServerURL = "http://flickr.xxx.com/index.php"			var EmailAddress = "obuckley@xxx.com"		var URL = "www.analogstudios.net"		// g_httpConn	g_httpConn = new XMLHttp();		g_httpConn.open("POST", httpServerURL , true);			//Send stuff	g_httpConn.send(EmailAddress);	g_httpConn.send(URL);}

Link to comment
Share on other sites

is there a way in PHP to output success or failure with some sort of XML markup that I could read with my app?
You could use XML if you want, you could also use JSON or another format, or even just check for the word "success".For this link:http://flickr.kenzanmedia.com/index.php/?U...kenzanmedia.comYou've got invalid characters in the values you're sending, that's what I mean by URL-encoding the characters. The @ sign has a special meaning in a URL, so if you're putting that in the value you need to escape it. If you're writing that code in Javascript, you can use the encodeURIComponent function for that. Also, when you send post data, it needs to be in the same format as the querystring. So you would build a querystring with the escaped values and then send that.
scene.Button2.onClick = function () {	var httpServerURL = "http://flickr.kenzanmedia.com/index.php"			var EmailAddress = encodeURIComponent("obuckley@kenzanmedia.com");		var URL = encodeURIComponent("www.analogstudios.net");	var data = "EmailAddress=" + EmailAddress + "&URL=" + URL;		// g_httpConn	g_httpConn = new XMLHttp();	g_httpConn.open("POST", httpServerURL , true);		//Send stuff	g_httpConn.send(data);}

Link to comment
Share on other sites

You could use XML if you want, you could also use JSON or another format, or even just check for the word \"success\".For this link:You\'ve got invalid characters in the values you\'re sending, that\'s what I mean by URL-encoding the characters. The @ sign has a special meaning in a URL, so if you\'re putting that in the value you need to escape it. If you\'re writing that code in Javascript, you can use the encodeURIComponent function for that. Also, when you send post data, it needs to be in the same format as the querystring. So you would build a querystring with the escaped values and then send that.
scene.Button2.onClick = function () {	var httpServerURL = \"http://flickr.xxx.com/index.php\"			var EmailAddress = encodeURIComponent(\"obuckley@xxx.com\");		var URL = encodeURIComponent(\"www.xxx.net\");	var data = \"EmailAddress=\" + EmailAddress + \"&URL=\" + URL;		// g_httpConn	g_httpConn = new XMLHttp();	g_httpConn.open(\"POST\", httpServerURL , true);		//Send stuff	g_httpConn.send(data);}

No luck unfortunately. I\'m using your new code suggestions above with this stripped down PHP code. I\'m guessing the language associated with this program is JSON? or javascript, but not sure which one. I get no errors in the program with any of this new code either, so I\'m guessing its recognizing everything I\'m throwing at it.
<?php	  //To	  $to = {$_POST[\'EmailAddress\']};  	  //Subject	  $subject = \'Flickr Authentication Request\';  	  //Create The Body	  $body = 		  \"Your Authentication URL is: 		  {$_POST[\'URL\']} \\n\";   	  //set the size of the body 	  $body = wordwrap($body, 100);  	  //Send the Email 	  mail($to, $subject, $body);	  //Thank You Message	  echo \'Thanks\';?>

Link to comment
Share on other sites

First, when you're debugging ajax you should be using something like Firebug with Firefox. With Firebug you can look at the ajax request going out and check what the response from the browser was. If the PHP code has an error in it and the response is a 500 response you can use Firebug to check the response text to see the error message. Or, you can also have PHP log all errors to a file and you can check the error log instead of looking for errors in the browser. Your code that sends the request out isn't doing anything with the response though, it's not checking what the response was. You can add that in if you want to show a message to the user depending on what the response from PHP was.

Link to comment
Share on other sites

So I've got a lot more of my app working, the last thing I need to be able to do is read a response from my PHP script. This is a Flickr_API app that has been able to read XML responses from Flickr and display it as string and whatnot in my app. I've also been able to send requests to my server and then have that emailed to me, like I had originally posted about. Now I just need my app to read a variable, essentially, from my PHP script, so that I can read it and manipulate it much in the same way that I have been able to do for a Flickr response. So basically, how would I go about turning a PHP variable into XML so that my app recognizes, OR, what can PHP do as far as returning a response?Would something like this get me going in the right direction?http://www.php.net/manual/en/function.xmlrpc-encode.phpedit: all set! :)

Link to comment
Share on other sites

I actually prefer to use JSON for ajax communication. JSON is the native Javascript notation for objects. Since Javascript is on the receiving end, having PHP print out native Javascript is a pretty easy way to go. PHP uses the json_encode function for that, so you can use that to encode an entire array and send it to Javascript.So you could have an array like this:

$ar = array(  'name' => 'Joe',  'age' => 30);

And after using json_encode it will output this:{'name':'Joe', 'age':30}That's a native Javascript object. If that's the entire response from the server, you can use eval in Javascript to convert that to an object and then just use it like a regular object:

var obj = eval('(' + response + ')');alert(obj.name + ', ' + obj.age);

Most of my applications actually use JSON both ways, PHP also has a json_decode function that will decode a JSON string into a PHP array or object. You can use JSON to represent any type of array or generic object.

Link to comment
Share on other sites

So this is what my working PHP ended up looking like

<?php	$url = $_GET['url'];	$emailAddress = $_GET['emailAddress'];	$headers = "From: admin@xxx.com";	  //To	$to = $emailAddress;	  //Subject	$subject = 'Flickr Authentication Request';	  //Create The Body	  $body = "Your Authentication URL is: $url . Please use this link to authenticate this session, and then return to the Flickr application.";		  //set the size of the body 	  //$body = wordwrap($body, 100);  	  //Send the Email 	  mail($to, $subject, $body, $headers);?><data>	<thanks>Message sent.  Please check your email.</thanks></data>

This came in handy for some the m5 hash() I needed to use too.My calls look like this

//now that we have our new API_sig, we need to make a build a login URL to Flickr for our user	var login_URL = "http://flickr.com/services/auth/?" + "api_key=" + api_key + "&perms=" + perms_delete + "&frob=" + frob + "&api_sig=" + api_sig;	print("URL call to Flickr: " + login_URL);		//Send Login/Authentication URL to the user	  var emailURL = "http://flickr.xxx.com/WebContent/email.php"			var emailAddress = "obuckley@xxx.com";		var url = login_URL;		var completeURL = emailURL + "?emailAddress=" + emailAddress + "&url=" + encodeURIComponent(url);	//Send authentication email out to user	g_httpConn = new XMLHttp();	g_httpConn.open("GET", completeURL , false);		g_httpConn.send(completeURL);	if (g_httpConn.status == 200)	{			// take data from file and make XML DOM			// g_XmlDoc is a global XMLDocument 			g_XmlDoc = new XMLDocument;			// Get the responseText and load it into an XMLDocument			g_XmlDoc.loadString(g_httpConn.responseText);			// print something to show that XML loaded			confirmation = g_XmlDoc.documentElement.getElementsByTagName("thanks").item(0).text;						print(confirmation);						//Display email confirmation message			scene.InputBox1.setText(confirmation);	  }	  else	  {			print("load error =  " + g_httpConn.statusTest + "  " + g_httpConn.status); 	  }

Wow, what a great day at the office! Thanks for all your help and patience, I finally made it to the end. Now finishing the rest of my app should be a breeze. :)

Link to comment
Share on other sites

one more thing...I'm getting a response in XML format about a user's info, but I'm confused about how to get the specific info I need out of the return. This is the response and the values I have access to based on a certain call. I can get <token>, but I want to know how to get one or all the different values for user NSID, username, or fullname. This is what my response looks like.

<auth>	<token>976598454353455</token>	<perms>write</perms>	<user nsid="12037949754@N01" username="Bees" fullname="Cal H" /></auth>

and this is what has been able to get me individual bits and pieces...

	if (g_httpConn.status == 200){		g_XmlDoc = new XMLDocument;		g_XmlDoc.loadString(g_httpConn.responseText);		//Get Token		token = g_XmlDoc.documentElement.getElementsByTagName("token").item(0).text;						print ( "Token is: " + token);	}	else{		print("load error =  " + g_httpConn.statusTest + "  " + g_httpConn.status); 	}

Link to comment
Share on other sites

You should be able to get the user tags the same way you're getting the token tags, and then I think you'll get the nsid just as a property of the tag. e.g.:g_XmlDoc.documentElement.getElementsByTagName("user").item(0).nsid

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...