Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help transform PHP SMS sending to VFP 1

Status
Not open for further replies.

Ricardo.a4

IS-IT--Management
Feb 2, 2022
3
PT
Hello!
Sory my litle english !
Im trying to use the SMS gateway from smsgateway.me in a software that works in VFP.

Anyone can help me to implement the PHP code in VFP?

The code is in:
Code:

<?php

require 'vendor/autoload.php';

use SMSGatewayMe\Client\ApiClient;
use SMSGatewayMe\Client\Configuration;
use SMSGatewayMe\Client\Api\MessageApi;
use SMSGatewayMe\Client\Model\SendMessageRequest;

// Configure client
$config = Configuration::getDefaultConfiguration();
$config->setApiKey('Authorization', 'your-token-here');
$apiClient = new ApiClient($config);
$messageClient = new MessageApi($apiClient);

// Sending a SMS Message
$sendMessageRequest1 = new SendMessageRequest([
'phoneNumber' => '07791064781',
'message' => 'test1',
'deviceId' => 1
]);
$sendMessageRequest2 = new SendMessageRequest([
'phoneNumber' => '07791064781',
'message' => 'test2',
'deviceId' => 2
]);
$sendMessages = $messageClient->sendMessages([
$sendMessageRequest1,
$sendMessageRequest2
]);
print_r($sendMessages);


*************
Thanks for everthing
 
To say it simple, thisis just the toip of the iceberg.

If you're after using the same service of smsgateway.me, then you need to find the code within all of the PHP that does HTTP requests and that can be translated to VFP.

Before you post all the code in the SMSGatewayMe\Client\ folder, take a look at the LEFT side of
This in short tells you which http message body (JSON) to send with a HTTP POST request to
If this information isn't enough for you, as you're not only new to this API but HTTP APIs in general, google making a HTTP Post request. It's just a bunch of lines:

Code:
LOCAL loRequest, lcPOSTRequestbody

TEXT TO lcPOSTRequestbody NOSHOW
{
    "phone_number": "0123456",
    "message": "Hello World",
    "device_id": 1
}
ENDTEXT

loRequest = CREATEOBJECT("microsoft.xmlhttp")
loRequest.open("POST","[URL unfurl="true"]https://smsgateway.me/api/v4/message/send")[/URL]
loRequest.setRequestHeader("Content-Type","application/json")

loRequest.send(lcPOSTRequestbody)
DO WHILE loRequest.readyState<4
   Doevents
ENDDO

? loRequest.Responsetext

This will return {"status":"fail","message":"Could not process request","data":{"exception":"Invalid input"}}

And that's because it lacks preparation. You first need to define devices and care for configuraiton before the first send call works, beseides the docs have to specify how you send over your api key.

So as said, it's just the tip of the iceberg, you need to look under the sea surface to see the rest of the iceberg, too.


Chriss
 
Hi Chriss!
Thanks for the fast reply
I try to adapte anothers scripts that i found, but in this case i cant do nothing.
I have this working with another gateway.
Read the PHP, they do 2 things diferents, one is the API Code and after the code to send the Message
I never work with this Httprequest.. you can help me more please?
Regards

Code:
Local loHTTPRequest, lcUser, lcPassword, lcTet, lcParam, lcToken, lcMensagem, lcTelemovel

	lcUser     = "rose@rippb.com"
	lcPassword = "Rosebit"
	lcMensagem = "Para solicitar uma demonstração do Software PHC, clique aqui [URL unfurl="true"]http://www.phc.pt/portal/programs/gensel.aspx?wwf=WRKFDEMO"[/URL]
	lcToken    = "eyJ0eXAiOiJKV1LCJhbGciOiJIU1NiJ9.eyJpc3MiOiJhZG1pbiIsImlhdCI6MTY0MzgyMzA1NiwiZXhwIjo0MTAyNDQ0ODAwLCJ1aWQiOjkyODA1LCJyb2xlcyI6WyJST0xFX1VTRVIiXX0.0dHkfH2msJQ-Zn_R9FWhpPIy68-9znym-XHia1vbE"
	lcTelemovel = "939695914"

	Try
		loHTTPRequest = Createobject("MSXML2.XMLHTTP.6.0")
	Catch
    	loHTTPRequest = Createobject("MSXML2.XMLHTTP.3.0")
	Endtry

	loHTTPRequest.Open("POST","[URL unfurl="true"]https://smsgateway.me/API/V4/Auth?username="+lcUser+"&password="+lcPassword+"&expires=0")[/URL]
	loHTTPRequest.send(.NULL.)
	Do While loHttpRequest.readyState <> 4
		Doevents
    EndDo 

	local _position

	*Resposta do Servidor
	lcToken   = loHttpRequest.responseText
	*Encontrar o Token...
	_position = at("<td>Token:&nbsp;</td><td>",lcToken)
	lcToken   = substr(lcToken,_position+25,32)

	lcMensagem = URLEncode(lcMensagem)
	lcParam = "username="+lcUser+"&Message="+lcMensagem+"&Telephones="+lcTelemovel+"&Sender=Direct100&Type=SMS"
   
	loHTTPRequest.Open("POST","[URL unfurl="true"]https://smsgateway.me/API/V4/"+lcToken+"/V2/Sms?"+lcParam)[/URL]
	loHTTPRequest.send(.NULL.)
	Do While loHttpRequest.readyState <> 4
		Doevents
	EndDo 

	msg("OK!")

Return
 
While they are calling this an SMS gateway is it not actually more like a chat server mechanism?
I do a good bit of SMS work in my apps. Since my primary niche market is transportation phone communication is a very important task to get right. No worries I am getting to my point. I eventually opted to use Twilio SMS message platform. The issue was the calling code was more robust than FoxPro was able to deal with efficiently. I basically made a dotNet 4 wrapper dll to process SMS messages and record statuses. then I used my wrapper DLL in my VFP code and it worked like a champ. So maybe that same method would work for you here. And dotNet code examples are pretty easy to come by for the very API you are wanting to work with.


Steve Bowman
Independent Technology, Inc.
CA, USA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top