Make my AS3 code go fetch information on a website that I don't own

我的未来我决定 提交于 2019-12-14 02:53:31

问题


There is this website : http://www.swellmap.co.nz/ and I'd like to make my AS3 code go fetch some infos and displays it flash.

Is it possible if I don't own the website ?

Exemple :

I want to display these infos in my AS3 code. Is this possible ? How can I do ?

Thx for your help,


EDIT

Thx to the full answer of VC.One I've managed to paste infos in a String.

Here's what I did :

var myString:String;
var request:URLRequest = new URLRequest("http://www.swellmap.co.nz/ajr.php?r=plugin&a=Surfing&s=Anse%20Vata&country=nz&swellmap=1&country=ncd&swellmap=1&_=1460963404274"); 
var loader:URLLoader = new URLLoader(); 

loader.load(request);
loader.addEventListener(Event.COMPLETE,weatherLoaded);


function weatherLoaded(e:Event):void{
myString = e.target.data;
    trace(myString);  //output is {"tides":"High: 05:40 am (1.32 m); Low: 12:10 pm (0.57 m); High: 06:10 pm (1.19 m); ","seatemp":"27&deg;C","forecastdate":"17h","rating":"<img src='http:\/\/www.swellmap.co.nz\/style\/img\/weathericons\/1r.png' alt='Poor conditions' title='Poor conditions' \/>","rating_class":"<span class='badge badge-important' alt='Poor conditions' title='Poor conditions'>1<\/span>","summary":"<img class='wx-summary' src='http:\/\/www.swellmap.co.nz\/style\/img\/weathericons\/suncloud.png'  title='Sunny with some cloud' \/>","title":"Anse Vata","smaplink":"http:\/\/www.swellmap.co.nz\/surfing\/new-caledonia\/anse-vata","vars":{"hs_sw":{"value":"0.4","title":"Swell","unit":"m"},"hs":{"value":"0.6","title":"Wave","unit":"m"},"wface":{"value":"0.8","title":"Set face","unit":"m"},"tp":{"value":"13","title":"Period","unit":"s"},"dpm":{"value":"S","title":"Swell dir","unit":"&#xb0;"},"windma":{"value":"E 12","title":"Wind","unit":"kts"},"gstma":{"value":"16","title":"Gusts","unit":"kts"}}}
}

Now, I didn't quite understand how could I retrieve only some infos (like the swell for exemple). Is it possible to show me in AS3 code, how could I do that ? (in this exemple, we can see that the swell is "0.4 m @ 13 s")

exemple of what I'd like to do :

function(searchTheSwell){
var swell_AnseVata;
swell_AnseVata =.... ?
info_txt.text = swell_AnseVata;
}

回答1:


If you use the Developer Tools of your browser then you'll see that there's a PHP file being accessed to get the information. It starts http://www.swellmap.co.nz/ajr.php?r= and you need to find what it says for you. Now to view the contents just remove the part of the URL with &callback=XYZ where XYZ will be whatever the link has..

1) Here's an example of how to get data for a location :

http://www.swellmap.co.nz/ajr.php?r=plugin&a=Surfing&s=ZZZZZ&country=nz&swellmap=1&country=YYY&swellmap=1&_=1460950764514

Replace &s=ZZZZZ with name of location, so if I want Anse Vata it becomes &s=Anse%20Vata and La Nera becomes &s=La%20Nera. So use %20 for any spaces in location name. Replace &country=YYY with example &country=fra.

2) To get the JSON data for Anse Vata, declare your new String variable to later hold the JSON text and then just use URLStream in AS3 to load the link (open in browser tab to check contents) :

http://www.swellmap.co.nz/ajr.php?r=plugin&a=Surfing&s=Anse%20Vata&country=nz&swellmap=1&country=ncd&swellmap=1&_=1460963404274

In the Event Complete listener function of URLStream you just use (example) myString = e.target.data;. Then use a JSON parser on your myString or do it manually yourself using String functions (like indexOf to word search).

3) If you opened the above link in an new tab you'll see the JSON entries you need to parse.

  • Swell : "hs_sw":{"value":"0.4","title":"Swell","unit":"m"} and for extracting the @ 13 s period use : "tp":{"value":"13","title":"Period","unit":"s"}
  • Wind : "windma":{"value":"E 12","title":"Wind","unit":"kts"}
  • Icon : "summary":"<img class='wx-summary' src='http:\/\/www.swellmap.co.nz\/style\/img\/weathericons\/suncloud.png'

    You'll have to clean up the icon links so it becomes for example : http://www.swellmap.co.nz/style/img/weathericons/suncloud.png

EDIT :

The code below extracts the expected information from the JSON string. Just replace &s= with any other location (eg: &s=Ilot%20Tenia) and it extracts the Swell, Wind and Icon URL entries...

var myURL : String = "http://www.swellmap.co.nz/ajr.php?r=plugin&a=Surfing&s=Anse%20Vata&country=nz&swellmap=1&country=ncd&swellmap=1&_=1460963404274";

var URL_Req : URLRequest  = new URLRequest( myURL );
var URL_load:URLLoader = new URLLoader();

URL_load.addEventListener(Event.COMPLETE, completeHandler);

try { URL_load.load( URL_Req ); } 
catch (error:Error) 
{ trace("Could not load - Please check URL is correct : " + error.message); }


var idx_start : int = 0;    var idx_end : int = 0;

var str_Swell : String = "";
var str_Swell_Period : String = "";
var str_Swell_Dir : String = "";
var str_Wind : String = "";     var url_Icon : String = "";


function completeHandler(e : Event):void 
{
    //var myData : Object = JSON.parse(e.target.data); 
    //for each (var s:* in myData) { trace("key:",s,"value:",myData[s]); }

    var myString : String = String(e.target.data);
    //trace ( "myString : " + myString);


    //# Get Swell
    idx_start = myString.indexOf("\"hs_sw\":" , 0 );
    idx_end = myString.indexOf("," , idx_start + 1 );
    str_Swell = myString.substring(idx_start + 18, idx_end-1);
    str_Swell = str_Swell + " m";
    //trace ("Swell : " + str_Swell );

    //# Get Direction (for Swell)
    idx_start = myString.indexOf("\"dpm\":" , 0 );
    idx_end = myString.indexOf("," , idx_start + 1 );
    str_Swell_Dir = myString.substring(idx_start + 16, idx_end-1);
    //trace ("Swell dir : " + str_Swell_Dir );

    //# Get time Period (for Swell)
    idx_start = myString.indexOf("\"tp\":" , 0 );
    idx_end = myString.indexOf("," , idx_start + 1 );
    str_Swell_Period = myString.substring(idx_start + 15, idx_end-1);
    str_Swell_Period = " @ " + str_Swell_Period + " s";
    //trace ("Period : " + string_Period );

    //# Join Swell Direction, Size & Period entries into one sentence
    str_Swell = str_Swell_Dir + " " + str_Swell + str_Swell_Period;
    trace ("Swell : " + str_Swell );

    //# Get Wind
    idx_start = myString.indexOf("\"windma\":" , 0 );
    idx_end = myString.indexOf("," , idx_start + 1 );
    str_Wind = myString.substring(idx_start + 19, idx_end-1);
    str_Wind = str_Wind + " kts";
    trace ("Wind :  " + str_Wind );

    //# get Image URL
    idx_start = myString.indexOf("\'wx-summary\'" , 0 );
    idx_end = myString.indexOf("'" , idx_start + 18 );
    url_Icon = myString.substring(idx_start + 18, idx_end);
    url_Icon = url_Icon.replace(/\\/g, "");

    trace ("URL : " + url_Icon );
    //# load image using : new URLRequest (url_Icon); 

}


来源:https://stackoverflow.com/questions/36684530/make-my-as3-code-go-fetch-information-on-a-website-that-i-dont-own

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!