API integrate by Curl with PHP/Web Services

Web Services
http://www.sourcecodemart.com/web-service-using-php-mysql-xml-and-json-for-beginners/
-----------------------------------------------------------------------------------------------
Example
__________
http://sendgrid.com/docs/Code_Examples/php_curl.html

________________________________________________________________
// $s_togglUrl is a client api
$s_togglUrl = '[client_api_url]?command=[commandname]&username=[username]&password=[password]&customer=[customer_username]&customerpassword=[customerpassword]';

$curl = curl_init();

$browser = $_SERVER['HTTP_USER_AGENT'];

$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";

$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";

$header[] = "Cache-Control: max-age=0";

$header[] = "Connection: keep-alive";

$header[] = "Keep-Alive: 300";

$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";

$header[] = "Accept-Language: en-us,en;q=0.5";

$header[] = "Pragma: ";

curl_setopt($curl, CURLOPT_FAILONERROR,true);

curl_setopt($curl, CURLOPT_USERAGENT, $browser);

curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');

curl_setopt($curl, CURLOPT_AUTOREFERER, false);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);

curl_setopt($curl, CURLOPT_TIMEOUT, 20);

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); //needed for SSL

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
      
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
   
curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem'); //path to file

curl_setopt($curl, CURLOPT_URL, $s_togglUrl);

$s_returnVal = curl_exec($curl);

//echo curl_error($curl);  //display errors under development

curl_close($ch);

//print_r( $s_returnVal );
$xml = simplexml_load_string($s_returnVal);
//print_r($xml);

Get Single tag xml data
_______________________
example:
<TestUserInfo>
  <Customer>Test</Customer>
  <SpecificBalance>123</SpecificBalance>

  <EmailAddress/>
</TestUserInfo>

$specific=$xml->SpecificBalance;

Get xml  data  with multiple tag
__________________________

example:
<CallOverview>
  <Calls Count="2">
    <Call Customer="this account" StartTime="2011-08-08 02:58:24" Duration="00:00:04" Charge="0.195" />
    <Call Customer="this account" StartTime="2011-08-08 02:55:38" Duration="00:00:07" Charge="0.116" />
  </Calls>

</CallOverview>



foreach($xml->Calls as $user)
{
    foreach($user->Call as $contact)
    {
        $StartTime = $contact->attributes()->StartTime;
        $Duration= $contact->attributes()->Duration;

    }
}

No comments:

Post a Comment