Accessing Blue America API Via PHP+CURL

207 views
Skip to first unread message

Jamie

unread,
Dec 9, 2009, 12:44:03 PM12/9/09
to ActBlue API Developers
I wanted to share the solution I came up with for accessing the
ActBlue API via PHP+CURL+SimpleXML. Needless to say this method
requires PHP 5.2+

First to obtain the XML file:

$curl = curl_init () ;
$header = array( "Accept: application/xml" );
curl_setopt ( $curl, CURLOPT_URL, $url ) ; // set to the URL of the
page you want
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // Required for
the insecure method used by actblue (-k/--insecure)
curl_setopt ($curl, curlOPT_SSL_VERIFYHOST, false);// Required for
the insecure method used by actblue (-k/--insecure)
curl_setopt ( $curl, CURLOPT_HTTPHEADER, $header ) ;
curl_setopt ( $curl, CURLOPT_AUTOREFERER, false ) ;
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ) ;
curl_setopt ( $curl, CURLOPT_TIMEOUT, 20 ) ;
$html = curl_exec ( $curl ) ; // execute the curl command - return
false on error.
curl_close ( $curl ) ; // close the connection

ActBlue doesn't always send back well formed XML and that can cause
SimpleXML to scream and shout at you. To fix that here is a simple
routine:

if (preg_match('/(<\?(.*?)>)/',$html,$matches)){
$html=$matches[0].str_replace($matches[0],'',$html);
}

After that you can simple load the $html into a new SimpleXML object
and extract the data that way:

$xml = simplexml_load_string ( $html ) ;

Hope this helps other PHP developers out!
Reply all
Reply to author
Forward
0 new messages