چاپارک را به نرمافزار خود متصل کنید
یک API، هزاران امکان
ارسال پیامک
ارسال پیامهای تبلیغاتی، خدماتی و اطلاعرسانی از داخل نرمافزار
دریافت پیامک
مدیریت پیامهای دریافتی و پاسخهای کاربران
مدیریت مخاطبین
افزودن، حذف و مدیریت شمارهها از طریق API
گزارشگیری
دریافت وضعیت ارسال و گزارش عملکرد پیامها
using System;
using System.IO;
using System.Net;
using Newtonsoft.Json;
namespace IdehPayam {
public enum ServiceType { SOAP = 1, REST = 2 };
public class UserCreditJsonResult
{
public int status { get; set; }
public int value { get; set; }
}
public class UserCreditJsonResponse
{
public int status { get; set; }
public UserCreditJsonResult result { get; set; }
}
class ClientApi {
private static string _restUrl = "http://localhost:8800/idehpayam/new-api/rest.php";
private static string _soapUrl = "http://localhost:8800/idehpayam/new-api/soap.php?wsdl";
private string _username = "";
private string _password = "";
private string _lastError = "";
private string _lastResponse = "";
public ClientApi(string username, string passsword)
{
this._username = username;
this._password = passsword;
}
public float GetUserCredit(ServiceType type)
{
switch (type) {
case ServiceType.REST:
var httpWebRequest = (HttpWebRequest)WebRequest.Create(ClientApi._restUrl);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) {
string json = "{\"operation\":\"GetUserCredit\", \"username\":\"" + this._username + "\", \"password\":\"" + this._password + "\"}";
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) {
UserCreditJsonResponse response = JsonConvert.DeserializeObject(streamReader.ReadToEnd());
this._lastResponse = response.ToString();
if (response.status == 200) {
if (response.result.status == 0) {
return response.result.value;
} else {
this._lastError = "Api status code " + response.result.status + " returned.";
}
} else {
this._lastError = "Status code is not OK";
}
System.Console.WriteLine(response.status);
}
return 0;
case ServiceType.SOAP:
return -1;
default:
throw new Exception("This service type is not valid.");
}
}
}
class MainClass {
public static void Main (string[] args) {
ClientApi client = new ClientApi("prodo", "123456");
float credit = client.GetUserCredit(ServiceType.REST);
Console.WriteLine(credit);
}
}
}
import requests
from zeep import Client, Settings
class IdehPayamClientApi:
TYPE_SOAP = 1
TYPE_REST = 2
soapUrl = 'http://localhost:8800/idehpayam/new-api/soap.php?wsdl'
restUrl = 'http://localhost:8800/idehpayam/new-api/rest.php'
def __init__(self, username, password):
self.username = username
self.password = password
def GetUserCredit(self, type):
if (type == self.TYPE_REST):
request = requests.post(self.restUrl, json={"operation": "GetUserCredit", "username": self.username, "password": self.password})
json = request.json()
if (json):
status = json['status']
if (status == 200):
innerStatus = json['result']['status']
if (innerStatus == 0):
return json['result']['value']
else:
print("Error code " + innerStatus + " returned.")
return 0
else:
print("Response status is not OK")
return 0
else:
print("Response error")
return 0
elif (type == self.TYPE_SOAP):
client = Client(self.soapUrl)
result = client.service.GetUserCredit(self.username, self.password)
if (result):
if (result['status'] == 0):
return result['value']
else:
print("Response status code " + result['status'] + " returned")
return 0
else:
print("Response error")
return 0
else:
raise Exception('This value for type is not supprted')
return 0
client = IdehPayamClientApi("prodo", "123456")
credit = client.GetUserCredit(IdehPayamClientApi.TYPE_SOAP)
print(credit)
_username = $username;
$this->_password = $password;
$this->_type = $type;
switch ($type) {
case self::$_TYPE_SOAP:
$this->_client = new SoapClient(self::$_soapUrl, [
'encoding' => 'UTF-8',
'connection_timeout' => 10,
'use' => SOAP_LITERAL,
'style' => SOAP_DOCUMENT,
'trace' => 1,
'exceptions' => 1
]);
break;
case self::$_TYPE_REST:
break;
}
}
public function GetLastError(): ?string
{
return $this->_lastError;
}
public function GetLastResponse(): ?string
{
return $this->_lastResponse;
}
/**
* Get user credit or null if fails
*
* @return float|null
*/
public function GetUserCredit(): ?float
{
$credit = null;
switch ($this->_type) {
case self::$_TYPE_SOAP:
try {
$response = $this->_client->__call('GetUserCredit', [
"username" => $this->_username,
"password" => $this->_password
]);
if (isset($response->status)) {
switch ($response->status) {
case 0:
return $response->value;
break;
}
} else {
$this->_lastError = 'Server error: status does not set.';
}
} catch (Exception $exp) {
$this->_lastError = 'Server error: ' . var_export($exp, true);
}
$this->_lastResponse = $this->_client->__getLastResponse();
break;
case self::$_TYPE_REST:
try {
$response = file_get_contents(self::$_restUrl, false, stream_context_create([
'http' => [
'method' => 'POST',
'header' => "Content-type: application/json",
'content' => json_encode([
'operation' => 'GetUserCredit',
'username' => $this->_username,
'password' => $this->_password,
])
]
]));
$this->_lastResponse = $response;
if ($responseObject = json_decode($response)) {
if (isset($responseObject->status) && isset($responseObject->result->status)) {
$credit = $responseObject->result->value;
} else {
$this->_lastError = 'Server error: status does not set.';
}
} else {
$this->_lastError = 'Server error: response is not json.';
}
} catch (Exception $exp) {
$this->_lastError = 'Server error: ' . var_export($exp, true);
}
break;
}
return $credit;
}
}
$client = new IdehPayamApiClient('prodo', '123456', IdehPayamApiClient::$_TYPE_REST);
$credit = $client->GetUserCredit();
if ($credit === null) {
echo $client->getLastError();
} else {
echo $credit;
}
const IdehPayamClientApi = function(username, password) {
this.restUrl = 'http://localhost:8800/idehpayam/new-api/rest.php';
this.soapUrl = 'http://localhost:8800/idehpayam/new-api/soap.php?WSDL';
this.ServiceType = Object.freeze({"SOAP": 1, "REST": 2});
this.username = username;
this.password = password;
this.getUserCredit = async (type = this.ServiceType.SOAP) => {
switch (type) {
case this.ServiceType.REST:
const response = await fetch(this.restUrl, {
method: 'POST',
body: JSON.stringify({ operation: "GetUserCredit", username: this.username, password: this.password }),
headers: { 'Content-Type': 'application/json' }
});
const jsonResponse = await response.json();
if (!jsonResponse) {
console.log("Response is not json");
return 0;
}
const status = jsonResponse.status;
if (!status) {
console.log("Response has not status key");
return 0;
}
const innerStatus = jsonResponse.result.status;
if (innerStatus == 0) {
return jsonResponse.result.value;
} else {
console.log("Response has invalid satus code: " + innerStatus);
return 0;
}
break;
case this.ServiceType.SOAP:
const soapResponse = await fetch(this.soapUrl, {
method: 'POST',
body:
'' +
' ' +
'' +
'' +
'' + this.username + ' ' +
'' + this.password + ' ' +
' ' +
' ' +
' ',
headers: { 'Content-Type': 'text/xml; charset=UTF-8', 'SOAPAction': 'GetUserCredit' }
});
const responseText = await soapResponse.text();
// console.log(responseText);
const responseData = (new window.DOMParser()).parseFromString(responseText, "text/xml");
const getUserCreditResponse = responseData.getElementsByTagName("ns1:GetUserCreditResponse");
if (getUserCreditResponse.length == 0) {
console.log("No GetUserCreditResponse element in response");
return 0;
}
const returnElm = getUserCreditResponse[0].getElementsByTagName("return");
if (returnElm.length == 0) {
console.log("No return element in response");
return 0;
}
const statusValue = returnElm[0].getElementsByTagName("status")[0].textContent;
if (statusValue == "0") {
return returnElm[0].getElementsByTagName("value")[0].textContent;
} else {
console.log("Server status code " + statusValue + " returned.");
return 0;
}
break;
default:
console.log('This type of request has not defined.');
break;
}
}
}
var client = new IdehPayamClientApi("prodo", "123456");
client.getUserCredit(client.ServiceType.SOAP).then((value) => {
console.log("credit: " + value);
});
package main
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"github.com/JalalJaberi/gosoap"
)
const (
TYPE_SOAP = 1
TYPE_REST = 2
)
type UserCreditResponse struct {
XMLName xml.Name `xml:"return"`
Status int64 `xml:"status"`
Value int64 `xml:"value"`
Description string `xml:"description"`
}
type GetUserCreditResponse struct {
XMLName xml.Name `xml:"GetUserCreditResponse"`
UserCreditResponse UserCreditResponse `xml:"return"`
}
type IdehPayamClientApi struct {
ServiceType int `default:1`
Username string `default:""`
Password string `default:""`
_lastError string `default:""`
_lastResponse string `default:""`
}
func (client IdehPayamClientApi) GetRestUrl() string {
return "http://localhost:8800/idehpayam/new-api/rest.php"
}
func (client IdehPayamClientApi) GetSoapUrl() string {
return "http://localhost:8800/idehpayam/new-api/soap.php?wsdl"
}
func (client IdehPayamClientApi) GetLastError() string {
return client._lastError
}
func (client IdehPayamClientApi) GetLastResponse() string {
return client._lastResponse
}
func (client IdehPayamClientApi) GetUserCredit() int {
if (client.ServiceType == TYPE_REST) {
var jsonStr = []byte(`{"operation": "GetUserCredit", "username":"` + client.Username + `", "password":"` + client.Password + `"}`)
httpRequsest, httpErr := http.NewRequest("POST", client.GetRestUrl(), bytes.NewBuffer(jsonStr))
if httpErr != nil {
client._lastError = httpErr.Error()
return 0
}
httpRequsest.Header.Set("Content-Type", "application/json")
httpClient := &http.Client{}
response, clientErr := httpClient.Do(httpRequsest)
if clientErr != nil {
client._lastError = clientErr.Error()
return 0
}
defer response.Body.Close()
// fmt.Println("Response status:", response.Status)
if response.Status != "200 OK" {
client._lastError = "Response status is not OK"
return 0
}
body, bodyError := ioutil.ReadAll(response.Body)
if bodyError != nil {
client._lastError = bodyError.Error()
return 0
}
// fmt.Println("response Body:", string(body))
client._lastResponse = string(body)
var data map[string]interface{}
if parseErr := json.Unmarshal(body, &data); parseErr != nil {
client._lastError = parseErr.Error()
return 0
}
statusField, statusIsSet := data["status"]
if (!statusIsSet) {
client._lastError = "Status is not set in response"
return 0
}
status := statusField.(float64)
if status != 200 {
client._lastError = "Status is not 200"
return 0
}
result := data["result"].(map[string]interface{})
innerStatus := result["status"]
if innerStatus.(float64) == 0 {
return int(result["value"].(float64))
} else {
client._lastError = "returned status: " + innerStatus.(string)
}
return 0
} else if (client.ServiceType == TYPE_SOAP) {
soap, soapErr := gosoap.SoapClient(client.GetSoapUrl())
if (soapErr != nil) {
client._lastError = soapErr.Error()
return 0
}
params := gosoap.Params{
"username": client.Username,
"password": client.Password,
}
response, callErr := soap.Call("GetUserCredit", params)
if (callErr != nil) {
client._lastError = callErr.Error()
return 0
}
client._lastResponse = string(response.Body)
var getUserCreditResponse GetUserCreditResponse
parseErr := response.Unmarshal(&getUserCreditResponse)
if (parseErr != nil) {
client._lastError = parseErr.Error()
return 0
}
if getUserCreditResponse.UserCreditResponse.Status == 0 {
return int(getUserCreditResponse.UserCreditResponse.Value)
} else {
client._lastError = "Server error code: " + string(getUserCreditResponse.UserCreditResponse.Status)
return 0;
}
}
return 0;
}
func main() {
client := IdehPayamClientApi{
Username: "prodo",
Password: "123456",
ServiceType: TYPE_SOAP,
}
credit := client.GetUserCredit()
fmt.Println(credit)
}
// package ir.idehpayam.api.sample;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.ParseException;
import org.json.simple.parser.JSONParser;
public class IdehPayamApiClient
{
private class IdehPayamApiAdapter
{
public static final int TYPE_SOAP = 1;
public static final int TYPE_REST = 2;
private static final String restUrl = "http://localhost:8800/idehpayam/new-api/rest.php";
private static final String soapUrl = "http://localhost:8800/idehpayam/new-api/soap.php?wsdl";
private int _type = 1;
private String _username = "";
private String _password = "";
// private Object _client = null;
private String _lastError = "";
private String _lastResponse = "";
public IdehPayamApiAdapter(String username, String password, int type) throws Exception
{
if (type != TYPE_SOAP && type != TYPE_REST) {
throw new Exception("type parameter out of bound");
}
_username = username;
_password = password;
_type = type;
switch (type) {
case TYPE_SOAP:
break;
case TYPE_REST:
break;
}
}
public String GetLastError()
{
return _lastError;
}
public String GetLastResponse()
{
return _lastResponse;
}
public long GetUserCredit()
{
long credit = 0;
switch (_type) {
case TYPE_SOAP:
try {
/*$response = $this->_client->__call('GetUserCredit', [
"username" => $this->_username,
"password" => $this->_password
]);
if (isset($response->status)) {
switch ($response->status) {
case 0:
return $response->value;
break;
}
} else {
$this->_lastError = 'Server error: status does not set.';
}*/
} catch (Exception exp) {
_lastError = "Server error: " + exp.toString();
}
break;
case TYPE_REST:
try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
final HttpPost httpPost = new HttpPost(restUrl);
StringEntity params = new StringEntity("{\"operation\":\"GetUserCredit\",\"username\":\"" + _username + "\", \"password\":\"" + _password + "\"}");
httpPost.setEntity(params);
try (final CloseableHttpResponse response = httpclient.execute(httpPost)) {
// System.out.println(response.getCode() + " " + response.getReasonPhrase());
final HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity);
_lastResponse = responseString;
JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject)parser.parse(responseString);
Long status = (Long)jsonObject.get("status");
if (status == null) {
_lastError = "Server error: status does not set.";
} else {
credit = (long)((JSONObject)jsonObject.get("result")).get("value");
}
EntityUtils.consume(entity);
} catch (Exception exp) {
System.out.println(exp);
_lastError = "Server error: " + exp.toString();
}
} catch (Exception httpExp) {
System.out.println(httpExp);
_lastError = "Server error: " + httpExp.toString();
}
break;
}
return credit;
}
}
public static void main(String[] args) throws IOException, InterruptedException
{
try {
IdehPayamApiAdapter adapter = new IdehPayamApiClient().new IdehPayamApiAdapter("prodo", "123456", IdehPayamApiAdapter.TYPE_SOAP);
long credit = adapter.GetUserCredit();
System.out.println(credit);
} catch (Exception exp) {
System.out.println(exp.toString());
}
}
}
نمونه کد برای ارسال پیامک Verify
شاید بخواهید پیامکهای تأیید هویت را بهصورت خودکار در سایت یا اپلیکیشن خود ارسال کنید.
با استفاده از API چاپارک میتوانید فرآیند ارسال کدهای یکبار مصرف (OTP) را بهسادگی پیادهسازی کرده و تجربهای سریع و امن برای کاربران خود ایجاد کنید.
سرویس Verify چاپارک به شما امکان میدهد کدهای تایید را در کوتاهترین زمان ممکن برای کاربران ارسال کنید و ورود، ثبتنام و احراز هویت را بدون پیچیدگی مدیریت نمایید.
ارسال سریع پیامک
با استفاده از این سرویس، کدهای تایید در سریعترین زمان ممکن به شماره کاربران ارسال شده و فرآیند احراز هویت با سرعت و دقت بالا انجام میشود.
امنیت و اطمینان بالا
ارسال کدهای یکبار مصرف به کاهش خطاهای امنیتی کمک کرده و امکان تایید شماره کاربران را به شکلی مطمئن فراهم میکند.
پشتیبانی از زبانهای برنامهنویسی مختلف
نمونه کدهای آماده برای زبانهای محبوب برنامهنویسی مانند CSharp، Python، PHP، JavaScript، Golang و Java در اختیار شما قرار گرفته است تا اتصال API به سادگی انجام شود.
اتصال آسان به سیستم شما
با استفاده از کلید API و مستندات کامل چاپارک، میتوانید سرویس پیامکی خود را در کمترین زمان به وبسایت، نرمافزار یا اپلیکیشن خود متصل کنید.
سازگار با تکنولوژیهای محبوب