Announcement

Collapse
No announcement yet.

Calling web page for data

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calling web page for data

    Greetings:

    I'm trying to retrieve a text string from a URL using the following:

    function preMain()
    {
    setPriceStudy(true);
    setStudyTitle("Proof");
    setCursorLabelName("Proof");
    }

    var ReportDataCall = null;
    var ReportData = null;
    var ReportDataArray = new Array();

    function main()
    {
    // Get Report data from website

    if (getBarState() == BARSTATE_NEWBAR)
    {
    ReportDataCall = new HTTP("www.tradersreports.com/reportdata/report.txt");
    if (ReportDataCall == null) return;

    ReportDataCall.open("rt");
    ReportData = ReportDataCall.readln();
    }

    debugClear();
    debugPrintln(" ReportData: " + ReportData);
    }

    Unfortunately, the variable 'ReportData' does not return any value. I've tried many variations to no avail.

    Any ideas?

    Warren

  • #2
    Warren:

    Two issues:

    1. HTTP() is a relatively high-resource function and you definitely do not want to call it on each iteration of main(). Instead you should set up your logic so that it is only called when needed (i.e., once when the script is first loaded or only under specific circumstances.

    2. In terms of the URL that you pass to the HTTP function you need to include the http:// prefix AND you need to provide the URL in exactly the same case (i.e., upper-case, lower-case or some combination) as it exists on the web site you are accessing.

    As an example of both points above, the following code will work:

    PHP Code:
    var bInitialized false;

    function 
    preMain() 
    {
    setPriceStudy(true);
    setStudyTitle("Proof"); 
    setCursorLabelName("Proof");
    }

    function 
    main() 
    {
    var 
    s;
    var 
    ReportDataCall null;
    // Get Report data from website

        
    if ( bInitialized==false ) {
        
            
    ReportDataCall = new HTTP"http://www.TradersReports.com/Reportdata/report.txt" );
            
    with ReportDataCall ) {
                
    ret open"rt" );
                
    s=null;
                 if ( 
    ret==true ) {
                     while ( !
    eof() ) {
                         
    ReportDataCall.readln();
                         if ( 
    != null ) {
                             
    debugPrint"\n" );
                         }
                     }
                 }
             }
            
            
    bInitialized true;
            
        }




    Chris

    Comment


    • #3
      Chris:

      I wasn't aware of the case sensitive requirement in the URL, so it works fine now that I know.

      Thanks for the help.

      Warren

      Comment

      Working...
      X