dcsimg
www.webdeveloper.com
Results 1 to 11 of 11

Thread: Trying to access Project Votesmart API

  1. #1
    Join Date
    Oct 2009
    Posts
    7

    Trying to access Project Votesmart API

    Hi, everyone. I am not well-versed in js. I need to access Project Votesmart's API to extract some data. The coding ought to be VERY easy, and I am sure it is. It's just that I have to start at the "Hello World" section in the js tutorials and go from there. It's a lot to have to cover to wind up with the needed 30-50 lines of code I need.

    Basically, here's what I want:

    getCandidateId(officeId, stateId, stateDistrict)

    This should return a string containing a candidate's id number. The parameters are compliant with Project VoteSmart's data structure. For example, officeId of "8" = state legislators, stateId of "TX" = "Texas," and stateDistrict of "16" is the state House District 16.

    Therefore, getCandidateId("8","TX","16") should return the Candidate Id Number assigned by PVS to Rep. Brandon Creighton, who is over state House District 16 in Texas.

    Here is a link to PVS's class structure: http://api.votesmart.org/docs/Address.html All the various objects are listed on the right, and if you click on one on the right, you will see more infrormation about it on the left, including the various ways you can do a search through the API.

    Note that all classes do not have the same structure. In the above example, we would use the Officials class, where we could getByOfficeState("8","TX"). This will give us an object that will include ALL legislators for the state in, for lack of a better description, a giant array.

    From there, we can search the officeDistsrictId elements until we hit "16." This will stop us at Creighton's record. That record includes a field, "candidateId." PVS assigns every official a candidateId.

    If we extract candidateId for Creighton, we can return that value, and the function GetCandidateId() is complete.

    PVS requires that you log in and request an access key. The access key allows you to access their data, much like a password.

    Here is the registration section: http://votesmart.org/user_register.php

    Thanks in advance for whoever is brilliant enough to solve this. I was a Clipper developer almost 20 years ago back in the DOS days, so I know the coding couldn't be all that much. The challenge is to visually see how to do this and to know the proper syntax to make the code concise and readable.

    Many thanks if you read this far!

  2. #2
    Join Date
    Jan 2009
    Posts
    3,346
    You can't do that with javascript by itself since it will be breaking the crossdomain security policy.

    http://api.votesmart.org/docs/libraries.html

    Try using one of the languages offered to you as options at the link above. They even provide you the basic code in PHP and walk you through how to set it up.

  3. #3
    Join Date
    Oct 2009
    Posts
    7
    Thanks, Criterion.

    I was just programming some js and you guessed EXACTLY where I would stop.

    I need to load the file from VSAPI.

    I am going to use this data in a KML file, which is the file type used by Google Earth. KML is basically an XML file, and GE specifies that certain tags, such as <Placemark> are interpreted and handled by GE in a certain way.

    There is a <description> tag GE uses, and everything within it can be displayed in a pop-up balloon when a user clicks on a placemark on a map.

    <description> tag supports javascript by way of using <![CDATA[

    So, I can reference only <script>.....</script>

    I will use within <script> a src=jsfilename, and then, it should run my js file.

    You are right in that VSAPI comes with PHP examples. I have used those.

    VS uses the PHP function file_get_contents() to go get the file.

    Is there any way I can use the PHP code in my js file? If so, how?

    Thanks, again, for your reply.

  4. #4
    Join Date
    Jan 2009
    Posts
    3,346
    You'll need to use the PHP code. You can call that code using AJAX but you will still need to use the PHP code that runs on the server instead of the javascript that runs in the browser.

  5. #5
    Join Date
    Oct 2009
    Posts
    7
    OK.

    So, you're saying that AJAX will not allow me to access PVS's API due to crossdomain security limitations? (I am posting some script below to show what I have so far.)

    If I can't use AJAX to do so directly, you are saying use AJAX to call a php file on my domain, having that PHP file access PVS's API? If so, how does this work? I assume PHP will open the database and return the XML, but how does it deliver that XML object over to JS?

    Here is my code:

    x= new OfficialsGetByOfficeState("8","TX");

    function OfficialsGetByOfficeState(officeId, stateId)
    {
    iface = "/Officials.getByOfficeState";
    args = "?key=" + VS_KEY + "\&o=" + "XML"+ "\&officeId="+officeId+"\&stateId="+stateId;

    xmlhttp=GetXmlHttpObject();
    xmlhttp.open("GET","http://api.votesmart.org"+iface+args,true);
    xmlhttp.send(null);
    return xmlhttp;
    }


    If I use the php code, instead, here is what I have:


    <?php

    require_once("config.php");

    class District {

    public function getByOfficeState($office_id, $state_id) {

    $iface = "/District.getByOfficeState";
    $args = "?key=" . _KEY_ . "&o=" . _OUTPUT_ . "&officeId=" . $office_id . "&stateId=" . $state_id;

    if (!$xml = file_get_contents(_APISERVER_ . $iface . $args)) {

    return false;

    } else {

    // Let's use the SimpleXML to drop the whole XML
    // output into an object we can later interact with easilly
    $xml_object = new SimpleXMLElement($xml);

    return $xml_object;

    }

    }

    }

    ?>




    So, php's function (I know it's accessing different data) returns xmlobject. But what would it return it to? In other words, what code would you write in JS to have it call the php function, along the lines of x=returnvalueofphpfunction()?


    I have since learned that their is a DOM for AJAX that lets you use methods, like getElementsByTagName(). But you are syaing this will not work using AJAX to access PVS's API directly - i.e. in my JS code above, x=OfficialsGetCandidateByOfficeState() returns nothing?

  6. #6
    Join Date
    Jan 2009
    Posts
    3,346
    You cannot access it directly. You will have to use a server-side script to grab the data from the remote server and give it to the javascript.

    http://en.wikipedia.org/wiki/Ajax_%28programming%29
    http://www.w3schools.com/Ajax/Default.Asp

  7. #7
    Join Date
    Oct 2009
    Posts
    7
    OK, I see. The tutorial I used on Ajax did not mention that limitation. It did, however, reference only local files for its url's.

    So, let's suppose I call a php script from my js script. Just a few easy questions that might take me hours to find without just a quick answer from a pro.

    1. What is the syntax for js command or function that calls the php script?
    2. Can you set a variable in a js script that accepts the return value of the php function (i.e. in js, x=returnvalueofphpfunction()?
    3. If not, what is the syntax in both js and php to insure that they share variable information?
    4. In my php code sample above, is it sufficient to pass back xml_object to js, or will I need to work further with the php to get the return value back down to a string value that I can return back? (i.e. instead of passing back the whole object to js, do I need to just get something, like candidateLastName, and return it back?

    Thanks for all your help.

  8. #8
    Join Date
    Oct 2009
    Posts
    7
    Here is a description of how the <description> tag works in Google Earth's KML format:

    http://code.google.com/apis/kml/docu...ml#description


    As you can see, js is supported. PHP, apparently, is not. So, I want to:

    1. In the <description> tag, insert the following: <![CDATA[<script src="myjssource"></script>]]></description>

    2. Of course, I want the <description> tag to say something that the js returns.

    3. The traditional use of the tag w/o scripting is, <description>Here is a placemark</description>

    4. CDATA with scripting allows you to fancify the text sent back to the description.

    5. I want to use CDATA and scripting to translate the CDATA to a new string, such as Candidate First Name, address, etc., mixed with some formatting tags.

    6. I can insert the formatting tags because CDATA allows that. All I need to do is go out and get the data and put it between the formatting tags.

    7. Google Earth's KML allows certain variables to be defined, such as StateDistrict. In the <description> tags, if you wanted to display it, you'd write: <description>$[StateDistrict]</description>.

    8. However, that is not enough. I need to use the StateDistrict variable, send it as a parameter to a js script, have the js script invoke the php that goes to the API and looks up the data I want using StateDistrict that I supplied, and return the other data back and display it. This other data would be name, address, phone, etc.

    9. Now, when a ballon pops up as the user clicks on a map's placemark, it will not just display the district number, it will display all sorts of information on the candidate provided from the VoteSmart API.

    I hope that is a clear description.

  9. #9
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by jeffmatthews View Post
    OK, I see. The tutorial I used on Ajax did not mention that limitation. It did, however, reference only local files for its url's.

    So, let's suppose I call a php script from my js script. Just a few easy questions that might take me hours to find without just a quick answer from a pro.

    1. What is the syntax for js command or function that calls the php script?
    2. Can you set a variable in a js script that accepts the return value of the php function (i.e. in js, x=returnvalueofphpfunction()?
    3. If not, what is the syntax in both js and php to insure that they share variable information?
    4. In my php code sample above, is it sufficient to pass back xml_object to js, or will I need to work further with the php to get the return value back down to a string value that I can return back? (i.e. instead of passing back the whole object to js, do I need to just get something, like candidateLastName, and return it back?

    Thanks for all your help.
    The links I gave you above should answer all those questions.

  10. #10
    Join Date
    Oct 2009
    Posts
    7
    Thanks, Crite.

    I am sure they do, but the industry lingo is giving me some fits.

    AAMOF, I read the whole AJAX tutorial from W3schools.

    Regarding these server issues, the only thing I see that seems to remotely apply is these 2 page from the tutorial:

    http://www.w3schools.com/Ajax/ajax_server.asp
    http://www.w3schools.com/Ajax/ajax_serverscript.asp

    The js script to open the server file is the following 2 lines:

    xmlhttp.open("GET","time.asp",true);
    xmlhttp.send(null);

    It then says that time.asp contains the following:

    <&#37;
    response.expires=-1
    response.write(time)
    %>

    I have no clue what an asp file is, but I assume it is the file on the server being opened by the js for access.

    The code in time.asp has <% and %> tags. I have no clue what they are, nor do I know if those lines of code in the asp are javascript or some other (asp?) language.

    These are just basic questions, but trying to find answers by Googling is very hit and miss.

    If there was something else pertinent in W3schools Ajax tutorial that I missed, please refer me to it. I feel like I understood the Ajax tutorial somewhat well. It's just there are certain things it fails to state (like the fact it will not access a remote server).

    Thanks for bearing with me. Believe it or not, I used to be a very avid (and proficient) Clipper programmer back in the DOS days. I know logic, operators, functionality, etc. very well. It's just this is a new paradigm that has a twist. I need to understand its basic concept. From there, the actual logic (code blocks) will be easy.

  11. #11
    Join Date
    Jan 2009
    Posts
    3,346
    Basically the AJAX concept for this case would be like:

    1) Someone loads your page with AJAX code in it
    2) The event is fired to call the data from the server (could be a click, timer, or any other event)
    3) The Javascript makes a call to a server-side page such as PHP, ASP, or JSP (they are all different languages...PHP is probably the most common and is also the one used in the example from the site you provided)
    4) The server-side page calls for the data you want from the API
    5) The server-side page can return xml, JSON, or plain text
    6) The javascript callback function receives the data and displays it however you see fit

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles