|
[WD22] intégrer un API en PHP |
Débuté par Kanigi Yaya, 14 juil. 2017 13:52 - 7 réponses |
| |
| | | |
|
| |
Membre enregistré 26 messages Popularité : +1 (1 vote) |
|
Posté le 14 juillet 2017 - 13:52 |
Bonjour à tous. Je veux intégrer un API de SMS dans mon application développée avec Windev22. Mes 2 scripts php ci-dessous
//PHP1 ****************************************util.php**************************************** <?php
define ('URL', '');
define ('TIME_STAMP', date('Y-m-d H:i:s'));
define ('LOGIN', 'test'); //Votre Login de connexion à l'interface WEB
define ('API_KEY', ''); //Votre apiKey qui est envoyé via SMS
define ('TOKEN', md5(LOGIN.API_KEY.TIME_STAMP));
define ('CHECK_ACCOUNT', 'checkaccount_v2/');
define ('SEND_SMS', 'sendsms/');
?>
//PHP1 ****************************************sms.php **************************************** <?php
include('util.php'); $recipients = ''; $sendmode = 0; $message = 'test 1'; $smstype = 'normal'; $sendername = '';
$curl = curl_init(URL.SEND_SMS); curl_setopt($curl, CURLOPT_FAILONERROR, FALSE); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_FAILONERROR, FALSE); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($curl, CURLOPT_POSTFIELDS, "recipients=$recipients&sendmode=$sendmode&message=$message&smstype=$smstype&sendername=$sendername"); curl_setopt($curl, CURLOPT_HTTPHEADER, array('apiKey: '.API_KEY, 'login: '.LOGIN, 'timeStamp: '.TIME_STAMP, 'token: '. TOKEN)); $response = @curl_exec($curl); curl_close($curl); print $response;
?>
-- Bon Dev! |
| |
| |
| | | |
|
| | |
| |
Membre enregistré 26 messages Popularité : +1 (1 vote) |
|
Posté le 14 juillet 2017 - 13:59 |
J'ai essayé ça mais sans succès!
Procedure PG_SMS_Send()
WS_Requete est restRequête WS_Reponse est restRéponse
WS_Requete.URL="http://xxxxxxxxxxxxxxxxxx/sendsms/" WS_Requete.Méthode=httpPost WS_Requete.ContentType="application/json" WS_Requete.Entête["API_KEY"]="monapikey" WS_Requete.Entête["LOGIN"]="monlogin" WS_Date est DateHeure=DateHeureSys WS_Requete.Entête["TIME_STAMP"]=DateVersChaîne(WS_Date..PartieDate,"JJ/MM/AAAA")+" "+HeureVersChaîne(WS_Date..PartieHeure,"HH:MM:SS") WS_Concat est une chaîne=WS_Requete.Entête["LOGIN"]+WS_Requete.Entête["API_KEY"]+WS_Requete.Entête["TIME_STAMP"] WS_Concat=HashChaîne(HA_MD5_128,WS_Concat) WS_Token est une chaîne POUR i = 1 _A_ Taille(WS_Concat) WS_Token+= NumériqueVersChaîne(Asc(WS_Concat[[i]]), "02x") FIN WS_Token=Minuscule(WS_Token) WS_Requete.Entête["TOKEN"]=WS_Token
WS_Contenu est une chaîne=[ {"message":"Test", "recipients":"+00001100110", "sendMode":"0", "smsType":"NORMAL", "senderName":"moi", "sendDate":"%1"} ] WS_Contenu=ChaîneConstruit(WS_Contenu,Gauche(DateHeureSys,14))
WS_Requete.Contenu=ChaîneVersUTF8(WS_Contenu)
WS_Reponse=RESTEnvoie(WS_Requete)
Info(WS_Reponse.Contenu)
Retour JSON : {"smsResponse":{"status":"failed","code":12,"desc":"missing parameter(s) in header"}}
Please hellllppppp!
-- Bon Dev! |
| |
| |
| | | |
|
| | |
| |
Membre enregistré 950 messages Popularité : +53 (63 votes) |
|
Posté le 14 juillet 2017 - 17:06 |
Bonjour,
d'après ce que je vois dans ton code php,
Les header que tu mets en wlanguage ne sont pas bon, le webservice attend 'apiKey' et non 'API_KEY', 'login' et non 'LOGIN', 'token' et non 'TOKEN', et 'timeStamp' et non 'TIME_STAMP', au niveau du timestamp tu met "JJ/MM/AAAA" alors qu'en php il est en "AAAA-MM-JJ"
je penses que ton problème vient de la
jordan |
| |
| |
| | | |
|
| | |
| |
Membre enregistré 26 messages Popularité : +1 (1 vote) |
|
Posté le 14 juillet 2017 - 21:25 |
Merci bien vu!! J'y vais.
-- Bon Dev! |
| |
| |
| | | |
|
| | |
| |
Membre enregistré 26 messages Popularité : +1 (1 vote) |
|
Posté le 14 juillet 2017 - 22:42 |
[Résolus] C'est bon ça marche merciiiiii !!!!iiiiii
WS_Requete.ContentType="application/json" WS_Contenu est une chaîne WS_Contenu ="recipients=0022000000&sendmode=0&message=Test&smstype=normal&sendername=moi"
-- Bon Dev! |
| |
| |
| | | |
|
| | |
| |
Posté le 13 juillet 2018 - 10:53 |
Bonjour Kanigi Yaya, Je veux faire la même chose que toi j'ai essayer mais ca marche pas est ce que vous pouvez m'aider ??
bon Dev. |
| |
| |
| | | |
|
| | |
| |
Membre enregistré 26 messages Popularité : +1 (1 vote) |
|
Posté le 12 septembre 2018 - 12:10 |
TOUATI Solution a écrit :
Bonjour Kanigi Yaya, Je veux faire la même chose que toi j'ai essayer mais ca marche pas est ce que vous pouvez m'aider ?? bon Dev.
Désolé de répondre 1 an plus tard. Que voulez-vous faire concrètement.
-- Bon Dev! |
| |
| |
| | | |
|
| | |
| |
Posté le 09 octobre 2018 - 15:07 |
Bonjour je suis nouveau je souhaite ce code PHP dans dans windev Mobile et je sollicite votre aide //////////////////////////////////////////////// ocr mobile//////////////////////////////////////////////////////////////////////////////////
<?php /* Warning : it is important to separate the curl connection in 3 differents functions to call API, otherwise the curl connection will not work */ $key1 = 'XXXXXXXXXXXXXXX'; $key2 = 'XXXXXXXXXXXXXXX';
$apiId = 'XXXXX-XXXXX-XXXXX-XXXXX-XXXXX'; $apiPassword = 'XXXXXXXXXXXXXXX';
$fileName = 'bill.pdf';
$userAgent = "ocrmobile.cloud";
$serviceUrl = 'https://www.ocrmobile.cloud/public/api/';
// get valid token to send treatment / valid during 3600 seconds $urlAuthenticate = $serviceUrl.'authenticate/'.$key1.'::'.$key2; // post scan or crop $urlTreat = $serviceUrl.'process/{token}'; // get result $urlResult = $serviceUrl.'getResult/{processId}/{token}';
// Get path to file that we are going to recognize $filePath = "/var/www/docs/".$fileName;
// 1. first step authenticate to get valid token for use api function authenticate(){ global $apiId,$apiPassword,$urlAuthenticate,$urlTreat,$urlResult,$userAgent,$filePath,$time_start; $curlHandle = curl_init(); curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curlHandle, CURLOPT_URL, $urlAuthenticate); curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curlHandle, CURLOPT_USERPWD, "$apiId:$apiPassword"); curl_setopt($curlHandle, CURLOPT_POST, 0); curl_setopt($curlHandle, CURLOPT_USERAGENT, $userAgent); curl_setopt($curlHandle, CURLOPT_FAILONERROR, true); $time_start= microtime(true); $response = curl_exec($curlHandle); $time_end = microtime(true); $execution_time = round(($time_end - $time_start)*1000,0);
echo $execution_time." ".$urlAuthenticate;
if($response == FALSE) { $errorText = curl_error($curlHandle); curl_close($curlHandle); die("authenticate error ".$errorText); } $httpCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE); curl_close($curlHandle); // reponse type JSON {"access":true,"token":"12346578","expires":3600} $jsonAuthenticate = json_decode($response,true); if($jsonAuthenticate["access"] === true){ $token = $jsonAuthenticate["token"]; $urlTreat = str_replace("{token}",$token,$urlTreat); $urlResult = str_replace("{token}",$token,$urlResult); }else{ die($jsonAuthenticate["error"]); }
} // 2. Send HTTP POST request and ret JSON response function sendProcess($processJson){ global $apiId,$apiPassword,$urlTreat,$userAgent,$filePath,$urlResult,$time_start; // echo " processJson : $processJson <br/>"; $curlHandle = curl_init(); curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curlHandle, CURLOPT_URL, $urlTreat); curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curlHandle, CURLOPT_USERPWD, "$apiId:$apiPassword"); curl_setopt($curlHandle, CURLOPT_POST, 1); curl_setopt($curlHandle, CURLOPT_USERAGENT, $userAgent); curl_setopt($curlHandle, CURLOPT_FAILONERROR, true); $post_array = array();
if((version_compare(PHP_VERSION, '5.5') >= 0)) { $post_array["my_file"] = new CURLFile($filePath); } else { $post_array["my_file"] = "@".$filePath; } // send params JSON see documentation datas.pdf $post_array["processJson"] = $processJson;// '{"supportId":2,"taxes":[2.1,5.5,10,20],"task":"'.$task.'","country":"FR","fileName":"'.$fileName.'","type":"img","srcCount":1,"useCrop":2,"saveBinary":true}'; curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $post_array); $response = curl_exec($curlHandle); if($response == FALSE) { $errorText = curl_error($curlHandle); curl_close($curlHandle); echo "response false ".$errorText; exit; } // var_dump($response); curl_close($curlHandle); $time_start = microtime(true); $jsonProcessResponse = json_decode($response,true); if($jsonProcessResponse["access"]){ if($jsonProcessResponse["access"] === false){ var_dump($jsonProcessResponse); exit; }else if(isset($jsonProcessResponse["error"])){ echo $jsonProcessResponse["error"]; exit; } else{ // get processId for request result $processId = $jsonProcessResponse["processId"]; $urlResult = str_replace("{processId}",$processId,$urlResult); }
}else{ echo $jsonProcessResponse["error"]; exit; }
}
// 3. get JSON process result function getResult(){ global $urlResult,$apiId,$apiPassword,$userAgent,$jsonResult; while(true) { sleep(1); $curlHandle = curl_init(); curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curlHandle, CURLOPT_URL, $urlResult ); curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curlHandle, CURLOPT_USERPWD, "$apiId:$apiPassword"); curl_setopt($curlHandle, CURLOPT_USERAGENT, $userAgent); curl_setopt($curlHandle, CURLOPT_FAILONERROR, true); $response = curl_exec($curlHandle); $httpCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE); curl_close($curlHandle);
$jsonResult = json_decode($response, true); if($httpCode != 200) { echo "Unexpected response getResult ".$response; $nbFalseResponse++; if($nbFalseResponse == 2){ echo $urlTreat."<br/>".$urlResult ; // exit; break; }else{ sleep(1); } } if($jsonResult["access"]){ if($jsonResult["status"] == "ok"){ // result treatment echo "<pre>".htmlentities($response)."</pre>"; break; }else if($jsonResult["status"] == "failed" || $jsonResult["status"] == "failed_"){ die("treatment fail ".implode("<br/>",$jsonResult)); }else if($jsonResult["status"] == "inProgress"){ continue; } }else{ die("No access ".$jsonResult["error"]); }
} }
// we call the 3 functions authenticate(); sendProcess('{"supportId":2,"taxes":[2.1,5.5,10,20],"task":"scan","language":"FR","country":"FR","fileName":"'.$fileName.'","type":"pdf","useCrop":true}'); getResult();
?> |
| |
| |
| | | |
|
| | | | |
| | |
|