大家好!特别是那些chikka sms api专家
我有一个问题花了我3个小时但仍然没有运气......
我决定在我们的顶点项目中选择chikka作为我的短信api ...
这是我在GitHUB中下载的代码,这里的用户在stackoverflow(Maverick)帖子中也发布了一个关于他的配置的帖子正在运行但我的不是......
这是代码:
使用example.php
<?php
/**
* This file will show a simple implementation on how to send SMS using Chikka API
* @author Ronald Allan Mojica
*
*/
include('ChikkaSMS.php');
$clientId = 'b43964ce5433fecdc4ee6fd7717xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$secretKey = '49502a1819b22c8465d5d6783d2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$shortCode = '29290xxxxxx';
$chikkaAPI = new ChikkaSMS($clientId,$secretKey,$shortCode);
$response = $chikkaAPI->sendText('1234561', '63906xxxxxxx', 'tests');
header("HTTP/1.1 " . $response->status . " " . $response->message);
exit($response->$description);
ChikkaSMS.php
<?php
/**
* Class ChikkaSMS Class handles the methods and properties of sending or receiving an SMS message.
* The main inspiration of this class was from Nexmo PHP Library
*
* Usage: $var = new NexoMessage ( $account_key, $account_password );
* Methods:
*
* sendText($requestId, $to, $message)
* receiveTxt()
* reply()
* receiveNotifications()
*
*/
class ChikkaSMS {
//authorization
private $clientId = '';
private $secretKey = '';
private $shortCode = '';
private $sslVerify = false;
//Chikka's default URI for sending SMS
private $chikkaSendUrl = 'https://post.chikka.com/smsapi/request';
private $sendRequest = 'send';
private $receiveRequest = 'incoming';
private $replyRequest = 'reply';
private $notificationRequest = 'outgoing';
//Based from Chikka's price breakdown
private $requestCost = array(
'free' => 'FREE',
'1' =>1,
'2.5'=> 2.5,
'5'=> 5,
'10' => 10,
'15' => 15
);
private $expectedChikkaResponse = array(
'message_type'=>'',
'short_code' => '',
'message_id' => '',
'status' => '',
'credits_cost' => '',
'timestamp' => '');
private $responseAccepted = array(
'status' => 'Accepted',
'message' => 'Message has been successfully processed.',
'code' => 202
);
private $responseDenied = array(
'status' => 'Error',
'message' => 'Message has not been processed.',
'code' => 400
);
/**
* [__construct description]
* @param [type] $clientId [description]
* @param [type] $secretKey [description]
* @param [type] $shortCode [description]
*/
public function __construct($clientId, $secretKey, $shortCode){
$this->clientId = $clientId;
$this->secretKey = $secretKey;
$this->shortCode = $shortCode;
}
/**
* SendText allows sending of SMS message to Chikka API
* @param type $requestId This identifier should be unique or your message will not be sent and you will be deducted
* @param type $to The mobile number you are sending an SMS
* @param type $message The SMS message
*/
public function sendText($messageID, $to, $message) {
$messageID = strip_tags($messageID);
//Request ID should not be blank
if(strlen($messageID) < 1){
trigger_error('Message ID is required');
return false;
}
// Making sure strings are UTF-8 encoded
if (!is_numeric($to) && !mb_check_encoding($to, 'UTF-8')) {
trigger_error('TO needs to be a valid UTF-8 encoded string');
return false;
}
if (!mb_check_encoding($message, 'UTF-8')) {
trigger_error('Message needs to be a valid UTF-8 encoded string');
return false;
}
//urlencode
$message = urlencode($message);
//sendText post params
$sendData = array(
'message_type' => $this->sendRequest,
'mobile_number' => $to,
'shortcode' => $this->shortCode,
'message_id' => $messageID,
'message' => $message
);
//send Api request to Chikka and process it
return $this->sendApiRequest($sendData);
}
public function receiveText() {
}
/**
* Reply - ability to send reply message
*
* @param [String] [requestID] [The requestID supplied by Chikka SMS]
* @param [String] [messageID] [Unique identifier]
* @param [String] [to] [mobile number starint 63]
* @param [String] [cost] [Amount to charge: Free, 1, 2.50, 5, 10, 15]
* @param [String] [message] [UTF-8 string]
*/
public function reply($requestID, $messageID, $to, $cost, $message) {
//Request ID should not be blank
if(strlen($messageID) < 1){
trigger_error('Message ID is required');
return false;
}
// Making sure strings are UTF-8 encoded
if (!is_numeric($to) && !mb_check_encoding($to, 'UTF-8')) {
trigger_error('TO needs to be a valid UTF-8 encoded string');
return false;
}
if (!mb_check_encoding($message, 'UTF-8')) {
trigger_error('Message needs to be a valid UTF-8 encoded string');
return false;
}
if (array_key_exists($cost, $this->requestCost)){
trigger_error('The cost value only allows FREE, 1, 2.5, 5, 10, and 15');
return false;
}
$message = urlencode($message);
//reply post params
$replyData = array(
'message_type' => $this->ReplyRequest,
'mobile_number' => $to,
'shortcode' => $this->shortCode,
'message_id' => $messageID,
'message' => $message,
'cost' => $this->requestCost[$cost],
'request_id' => $requestID
);
return $this->sendApiRequest($replyData);
}
/**
* [fetchNotifications description] removed the logic of showing Accepted and Error on receiving notification from Chikka API
* the operator should be the one doing it
*
*/
public function receiveNotifications() {
$fromChikka = $_POST;
if (count(array_diff_key($this->expectedChikkaResponse, $fromChikka)) != 0) {
$fromChikka = null;
}
return $fromChikka;
}
/**
* sendApiRequest - the functionality that sends request to Chikka API endpoint
* @param [array] $data post params
* @return [object]
*/
private function sendApiRequest($data){
$data = array_merge($data, array('client_id'=>$this->clientId, 'secret_key' => $this->secretKey));
// build a request query from arrays of data
$post = http_build_query($data);
// If available, use CURL
if (function_exists('curl_version')) {
$to_chikka = curl_init( $this->chikkaSendUrl );
curl_setopt( $to_chikka, CURLOPT_POST, true );
curl_setopt( $to_chikka, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $to_chikka, CURLOPT_POSTFIELDS, $post );
if (!$this->sslVerify) {
curl_setopt( $to_chikka, CURLOPT_SSL_VERIFYPEER, false);
}
$from_chikka = curl_exec( $to_chikka );
curl_close ( $to_chikka );
} elseif (ini_get('allow_url_fopen')) {
// No CURL available so try the awesome file_get_contents
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $post
)
);
$context = stream_context_create($opts);
$from_chikka = file_get_contents($this->chikkaSendUrl, false, $context);
} else {
// No way of sending a HTTP post :(
return false;
}
return $this->parseApiResponse($from_chikka, $data['message_type']);
}
/**
* parseApiResponse - process and handle Chikka api responses
* @param [array] $response Response from Chikka API
* @param [string] $requestType This is the message type of the sms
* @return [type]
*/
private function parseApiResponse($response, $requestType = null){
$response = json_decode($response,true);
if($requestType){
$response['request_type'] = $requestType;
}
return json_decode(json_encode($response));;
}
}
?>
这些是我遇到的错误
注意:未定义的属性:第15行的C:\ xampp \ htdocs \ ict \ sms \ example.php中的stdClass :: $ status
注意:第15行的C:\ xampp \ htdocs \ ict \ sms \ example.php中的未定义属性:stdClass :: $ message
注意:未定义的变量:第17行的C:\ xampp \ htdocs \ ict \ sms \ example.php中的描述
致命错误:无法在第17行的C:\ xampp \ htdocs \ ict \ sms \ example.php中访问空属性
答案 0 :(得分:0)
“这堂课的主要灵感来自Nexmo PHP Library”
相反,我建议使用Nexmo's SMS API,因为它只需要一个HTTP调用来发送短信。
如何发送短信(用PHP):
<?php
$url = 'https://rest.nexmo.com/sms/json?' . http_build_query([
'api_key' => API_KEY,
'api_secret' => API_SECRET,
'to' => YOUR_NUMBER,
'from' => NEXMO_NUMBER,
'text' => 'Hello from Nexmo'
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
答案 1 :(得分:0)
<?php
$arr_post_body = array(
"message_type" => "SEND",
"mobile_number" => "639181234567",
"shortcode" => "29290123456",
"message_id" => "12345678901234567890123456789012",
"message" => urlencode("Welcome to My Service!"),
"client_id" => "1e6d92a6e8794a7bb6aea67f008420e56a0272dfb21047369dc1ea0c9c8f8a19",
"secret_key" => "502f3b2ecce940f5b750dab07bf6b222de86f6e270a6427e9a0ea6b194bb4bdc"
);
$query_string = "";
foreach($arr_post_body as $key => $frow)
{
$query_string .= '&'.$key.'='.$frow;
}
$URL = "https://post.chikka.com/smsapi/request";
$curl_handler = curl_init();
curl_setopt($curl_handler, CURLOPT_URL, $URL);
curl_setopt($curl_handler, CURLOPT_POST, count($arr_post_body));
curl_setopt($curl_handler, CURLOPT_POSTFIELDS, $query_string);
curl_setopt($curl_handler, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($curl_handler);
curl_close($curl_handler);
exit(0);
?>
&#13;
参考:https://api.chikka.com/docs/getting-started#message-sender