Jump to content

API Connectivity


Tsalagi

Recommended Posts

Howdy. I"m trying to work with an API from Project Votes Smart but they have very little in the way of support and documentation. In the code example they provide there is a value already provided in the array located above the html code, which returns the information from the class it uses. There are two things that I'm stuck on.  I would like to know how to populate a drop down list from their array and how to pass data from a search form to the array so I can display information provided by the class. For instance instead of the "stateId" provided in the example, I would like the user to enter a zip code and pass that to the array. They have a location class that uses a zip code array.

Hope this isn't too confusing.

Thanks for the help.

<?php

require_once("VoteSmart.php");

// Initialize the VoteSmart object
$obj = new VoteSmart(

        'CandidateBio.getBio', 

        Array(
                'candidateId' => 9026
        ));

// Get the SimpleXML object
$x = $obj->getXmlObj();

echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- pwned -->
<html lang="en">
<body>';

// Check and make sure there is no error

if (isset($x->errorMessage)) { // If there is, let's handle it
        
        echo '
        <div>' . $x->errorMessage . '</div>';
        
} else { // If not, let's go ->
        
        // some quick and dirty assembly
        $candName = $x->candidate->firstName . ' ' . $x->candidate->middleName . ' ' . $x->candidate->lastName . ' ' . $x->candidate->suffix;
        if (!empty($x->candidate->photo)) $photo = '<img src="' . $x->candidate->photo . '" />';
        
        echo '
        <div>
                <a href="' . $x->generalInfo->linkBack . '">' . $x->generalInfo->title . '</a>
        </div>
        <br /><br />
        <table>
                <tr>
                        <td>Name</td><td>' . $candName . '</td>
                </tr><tr>
                        <td>Birth</td><td>' . $x->candidate->birthDate . ' (' . $x->candidate->birthPlace . ')</td>
                </tr><tr>
                        <td>Gender</td><td>' . $x->candidate->gender . '</td>
                </tr><tr>
                        <td>Photo</td><td>' . $photo . '</td>
                </tr><tr>
                        <td>Party</td><td>' . $x->office->parties . '</td>
                </tr><tr>
                        <td>Office</td><td>' . $x->office->name . '</td>
                </tr>
			<tr><td>District</td><td>' . $x->branch->name . '</td>
			</tr>
			<tr><td>State</td><td>' . $x->office->title . '</td>
			</tr>
                ';
        
}

echo '
</body>
</html>';

?>

Link to comment
https://forums.phpfreaks.com/topic/169857-api-connectivity/
Share on other sites

Howdy. I"m trying to work with an API from Project Votes Smart but they have very little in the way of support and documentation. In the code example they provide there is a value already provided in the array located above the html code, which returns the information from the class it uses. There are two things that I'm stuck on.  I would like to know how to populate a drop down list from their array and how to pass data from a search form to the array so I can display information provided by the class. For instance instead of the "stateId" provided in the example, I would like the user to enter a zip code and pass that to the array. They have a location class that uses a zip code array.

Hope this isn't too confusing.

Thanks for the help.

<?php

require_once("VoteSmart.php");

// Initialize the VoteSmart object
$obj = new VoteSmart(

        'CandidateBio.getBio', 

        Array(
                'candidateId' => 9026
        ));

// Get the SimpleXML object
$x = $obj->getXmlObj();

echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- pwned -->
<html lang="en">
<body>';

// Check and make sure there is no error

if (isset($x->errorMessage)) { // If there is, let's handle it
        
        echo '
        <div>' . $x->errorMessage . '</div>';
        
} else { // If not, let's go ->
        
        // some quick and dirty assembly
        $candName = $x->candidate->firstName . ' ' . $x->candidate->middleName . ' ' . $x->candidate->lastName . ' ' . $x->candidate->suffix;
        if (!empty($x->candidate->photo)) $photo = '<img src="' . $x->candidate->photo . '" />';
        
        echo '
        <div>
                <a href="' . $x->generalInfo->linkBack . '">' . $x->generalInfo->title . '</a>
        </div>
        <br /><br />
        <table>
                <tr>
                        <td>Name</td><td>' . $candName . '</td>
                </tr><tr>
                        <td>Birth</td><td>' . $x->candidate->birthDate . ' (' . $x->candidate->birthPlace . ')</td>
                </tr><tr>
                        <td>Gender</td><td>' . $x->candidate->gender . '</td>
                </tr><tr>
                        <td>Photo</td><td>' . $photo . '</td>
                </tr><tr>
                        <td>Party</td><td>' . $x->office->parties . '</td>
                </tr><tr>
                        <td>Office</td><td>' . $x->office->name . '</td>
                </tr>
			<tr><td>District</td><td>' . $x->branch->name . '</td>
			</tr>
			<tr><td>State</td><td>' . $x->office->title . '</td>
			</tr>
                ';
        
}

echo '
</body>
</html>';

?>

 

This issue has been solved by passing values to avariable with a form

thanks

Link to comment
https://forums.phpfreaks.com/topic/169857-api-connectivity/#findComment-896821
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.