コード例 #1
0
ファイル: client.php プロジェクト: kelvinmbwilo/cervical
 private function executeRequest($httpMethod, $url, $queryParams = null, $requestHeaders = null, $contentType = "application/x-www-form-urlencoded", $specialAuth = null)
 {
     if ($queryParams == null) {
         $queryParams = array();
     }
     if ($requestHeaders == null) {
         $requestHeaders = array();
     }
     // Check if the charset is specified in the content-type:
     if (strpos($contentType, 'charset') === false) {
         $charset = OneApiConfigurator::getCharset();
         if (!$charset) {
             $charset = 'utf-8';
         }
         $contentType .= '; charset=' . $charset;
     }
     $sendHeaders = array('Content-Type: ' . $contentType);
     foreach ($requestHeaders as $key => $value) {
         $sendHeaders[] = $key . ': ' . $value;
     }
     if ($httpMethod === 'GET') {
         if (sizeof($queryParams) > 0) {
             $url .= '?' . $this->buildQuery($queryParams);
         }
     }
     $opts = array(CURLOPT_FRESH_CONNECT => 1, CURLOPT_CONNECTTIMEOUT => 60, CURLOPT_TIMEOUT => 120, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 3, CURLOPT_USERAGENT => 'OneApi-php-' . self::VERSION, CURLOPT_CUSTOMREQUEST => $httpMethod, CURLOPT_URL => $url, CURLOPT_HTTPHEADER => $sendHeaders);
     if ($specialAuth) {
         $opts[CURLOPT_HTTPHEADER][] = 'Authorization: App ' . $specialAuth;
     } else {
         if ($this->oneApiAuthentication && $this->oneApiAuthentication->ibssoToken) {
             // Token based authentication (one request per login request):
             $opts[CURLOPT_HTTPHEADER][] = 'Authorization: IBSSO ' . $this->oneApiAuthentication->ibssoToken;
         } else {
             // Basic authorization:
             $opts[CURLOPT_USERPWD] = $this->username . ':' . $this->password;
         }
     }
     Logs::debug('Executing ', $httpMethod, ' to ', $url);
     if (sizeof($queryParams) > 0 && ($httpMethod == 'POST' || $httpMethod == 'PUT')) {
         $httpBody = null;
         if (strpos($contentType, 'x-www-form-urlencoded')) {
             $httpBody = $this->buildQuery($queryParams);
         } else {
             if (strpos($contentType, 'json')) {
                 $httpBody = json_encode($queryParams);
             }
         }
         Logs::debug('Http body:', $httpBody);
         $opts[CURLOPT_POSTFIELDS] = $httpBody;
     }
     $ch = curl_init();
     curl_setopt_array($ch, $opts);
     $result = curl_exec($ch);
     $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     if (curl_errno($ch) != 0) {
         throw new Exception(curl_error($ch));
     }
     $isSuccess = 200 <= $code && $code < 300;
     curl_close($ch);
     Logs::debug('Response code ', $code);
     Logs::debug('isSuccess:', $isSuccess);
     Logs::debug('Result:', $result);
     return array($isSuccess, $result);
 }
$smsMessage->senderAddress = SENDER_ADDRESS;
$smsMessage->address = DESTINATION_ADDRESS;
$smsMessage->message = 'Hell� world';
# ----------------------------------------------------------------------------------------------------
# example:send-message
$smsMessageSendResult = $smsClient->sendSMS($smsMessage);
# ----------------------------------------------------------------------------------------------------
//
# example:send-message-client-correlator
// The client correlator is a unique identifier of this api call:
$clientCorrelator = $smsMessageSendResult->clientCorrelator;
# ----------------------------------------------------------------------------------------------------
echo 'Response:', $smsMessageSendResult, "\n";
$deliveryStatus = null;
for ($i = 0; $i < 4; $i++) {
    # example:query-for-delivery-status
    // You can use $clientCorrelator or $smsMessageSendResult as an method call argument here:
    $smsMessageStatus = $smsClient->queryDeliveryStatus($smsMessageSendResult);
    $deliveryStatus = $smsMessageStatus->deliveryInfo[0]->deliveryStatus;
    echo 'Success:', $smsMessageStatus->isSuccess(), "\n";
    echo 'Status:', $deliveryStatus, "\n";
    if (!$smsMessageStatus->isSuccess()) {
        echo 'Message id:', $smsMessageStatus->exception->messageId, "\n";
        echo 'Text:', $smsMessageStatus->exception->text, "\n";
        echo 'Variables:', $smsMessageStatus->exception->variables, "\n";
    }
    # ----------------------------------------------------------------------------------------------------
    sleep(3);
}
OneApiConfigurator::setCharset(null);
Logs::printLogs();