PDA

View Full Version : simple php problem (but I don't know php)


natronp
06-02-2005, 03:14 AM
i'm trying to use a php file as a proxy so my flash movie can access web services on another domain.

this is working(!) as long as I hardcode the variable into my php script like this:

<?php

$dataURL = "http://www.innergears.com//WebServices/WeatherByZip/WeatherByZip.asmx/GetWeatherByZip?ZipCode=90210";

//note that this will not follow redirects
readfile($dataURL);



?>

but i'd love to use a variable so my homies can check the weather in their own zip code... I assume something like this, but I don't know php and how to correctly send the variable along with the URL:

<?php
var $ZipCode;
$dataURL = "http://www.innergears.com//WebServices/WeatherByZip/WeatherByZip.asmx/GetWeatherByZip?ZipCode=".$ZipCode;

//note that this will not follow redirects
readfile($dataURL);



?>

anybuddy out there who knows PHP feel like helpin me out?

Flash Gordon
06-02-2005, 03:48 AM
weather in their own zip code
See the webservice sticky

mmm..pi..3.14..
06-02-2005, 04:20 AM
yeah, second post in web service sticky is a tutorial on how to use web services in flash. That way you wouldn't even need PHP :D

The sticky is the first thread in the Actionscript 2.0 section (first forum on the list)

Eric

natronp
06-02-2005, 01:46 PM
Yeah, but i'm using mx (no webservice connector) and I wanted to code it anyway to learn more about it... I already have that thread bookmarked and that's where I got the weather service from! So thanks mm...pi... for posting it!!!

also, does the webservice connecter component allow you to access cross domain services? Doesn't flash's security sandbox prevent it or does the component bypass the sandbox?

what i have works i just need to pass the correct variable to php which can call the service from another domain for me.

i need to get mx2004.

mmm..pi..3.14..
06-02-2005, 06:57 PM
You can still use LoadVars to get around the fact that you have mx and not mx 2004.

Drag a text input box onto the stage. Give it the instance name "zip". Then create a button on the stage, give it the instance name "btn" (creative, eh?? :rolleyes: )

Now place this in your first frame actions:


btn.onRelease = function(){
var lv = new LoadVars()
lv.onLoad = function(ok){
if(ok){
trace(unescape(this))
}
}
lv.load("http://www.innergears.com/WebServices/WeatherByZip/WeatherByZip.asmx/GetWeatherByZip?ZipCode="+zip.text)
}


nothin fancy, but it should give you a start ;)

Eric

natronp
06-02-2005, 09:31 PM
Ok, thanks mm...pi..3.14...

I already had the flash built... I wasn't sure how to get that variable in the URL from the php file though.

the point of the php is that flash can't do a loadvars from yourdomain.com to mydomain.com.

The php file can access the web service though. at least that's the way it is with mx. the flashmx2004 webservices componenent might work around the flash security sandbox (? does it?)

at any rate I got the php working:
<?php
$dataURL = "http://www.innergears.com//WebServices/WeatherByZip/WeatherByZip.asmx/GetWeatherByZip?ZipCode=" . $zip;
//note that this will not follow redirects
readfile($dataURL);
?>

and here's my AS if anybody is interested:
stop();

//create loadvars object to send zipcode and an xml object to recieve output from web service
var SendZip = new loadVars();
var GetWeather = new XML();
GetWeather.ignoreWhite=true;
//when webservice data comes in do this
GetWeather.onLoad=function(){
textBox.text = GetWeather;
list=GetWeather.firstChild.childNodes;
var city=list[1].firstChild.nodeValue;
var state=list[0].firstChild.nodeValue;
var airport=list[7].firstChild.nodeValue;
var country=list[9].firstChild.nodeValue;
var todaydate=list[13].firstChild.nodeValue;
var winddir=list[14].firstChild.nodeValue;
var visib=list[15].firstChild.nodeValue;
var skies=list[16].firstChild.nodeValue;
var highs=list[17].firstChild.nodeValue;
var lows=list[18].firstChild.nodeValue;
var humidity=list[19].firstChild.nodeValue;
var barometric=list[20].firstChild.nodeValue;
SendZip.ZipCode="";
_root.location.text=city+", "+state;
_root.closestairport.text=airport;
_root.country.text=country;
_root.todaydate.text=todaydate;
_root.winddir.text=winddir;
_root.visib.text=visib;
_root.skies.text=skies;
_root.highs.text=highs;
_root.lows.text=lows;
_root.humidity.text=humidity;
_root.barometric.text=barometric;
trace(city);
trace(humidity);
trace(barometric);
}
//set an initial zip code to load
SendZip.zip=90210;
//trigger loadvars (to proxy.php page) assign container object GetWeather to recieve reply
SendZip.sendAndLoad("http://www.natepegman.com/proxy.php", GetWeather, "POST");
//set btn to take user input zipcode and trigger loadvars again
weatherBtn.onRelease=function(){SendZip.zip=UserZi p;
SendZip.sendAndLoad("http://www.mydomain.com/proxy.php", GetWeather, "POST");}

I was setting SendZip up as an xml object when it should have been a loadVars. I think I overlooked it b/c i knew that I needed an xml object to recieve from the web service... It shoud have been obvious... at any rate this works and the php was relatively simple to figure out. Thanks for all yer help!

p.s.- why are you creating a new loadvars object on a button event... seems like it would be better to create one and set up a function to trigger it. won't a new loadvars object be created each time the button is clicked? does that create more work? just curious...

mmm..pi..3.14..
06-02-2005, 09:41 PM
p.s.- why are you creating a new loadvars object on a button event... seems like it would be better to create one and set up a function to trigger it. won't a new loadvars object be created each time the button is clicked? does that create more work? just curious...

hmmm...never thought about why I do that. Just learned flash doing it like that, and it kinda stuck. Don't think theres any harm in doing either one of those methods though, personal preference I guess :)

Anyways...glad you got it working.

Eric

natronp
06-02-2005, 09:46 PM
yeah, maybe i should write a tutorial for all the people who have mx or who don't want to use the component. you never answered my question though.. does the component let you get that webservice when you upload your .swf to your domain (i.e.-cross domain)???

thanks mangk!

mmm..pi..3.14..
06-03-2005, 12:16 AM
yes it does. madgett is using the weather web service in his signature and it's hosted on a different domain than the web service is. Never run into a web service that wouldn't allow another domain to access it. That's the point of web services...to allow people in different places use it. If they didn't allow other domains to access them, they wouldn't be very useful :rolleyes:

natronp
06-03-2005, 01:17 AM
yeah, it's not the webservice that's the cross domain problem though it's the Flash security sandbox.

I don't have mx2004... so i've never used the component. all i know is that with straight actionscript alone you can't load data from another domain into flash (at least in FlashMX or older versions of the flashplayer)

In other words, it seems that in Flashmx you can load cross domain data
when you test but when you upload to your server you can't anymore...

is that true in MX2004 as well if you don't use the connector component/class?

I usually find that scripting what i want is easier than trying to figure out components... i need to upgrade to mx2004 though...

mmm..pi..3.14..
06-03-2005, 07:30 AM
never had a sandbox violation with web services through actionscript. You can check to be sure though. Just upload the fla to the web server where it will be when finished, then open up that fla in flash (if you can, if you don't have "physical" access to the server this won't work). When you test the fla which is running on the server, you'll either get an error in the output window or not. It works for me, but my server is only 20 feet away from my computer :rolleyes: