I am doing a script to send a string via URL link to an SMS gateway and get the response from them.
They will respond with a code and text for the answer, either '200 = SUCCESS' or an error-code: '-100 = UNKNOWN ERROR' , '-101 = MISSING PARAMETER' , etc.
In my back-end code, I would like to know how to keep looping, waiting for the return message from the SMS Gateway with an error-code and resend my string to the SMS gateway until success or confirmation?
$link = "http://www.smsgateway.com/send.php?phoneno=".$phonenumber."&message=".urlencode($smsMessage);
Next, I use cURL to send this link and get the response:
$returned_content = using_cURL_function_to_get_response($link);
Now, my $returned_content will show:
Success Code: 200 = SUCCESS
Failure Code: -100 = UNKNOWN ERROR
Failure Code: -101 = MISSING PARAMETER
and many more -1xx failure / error-codes
Then, I want to perform a loop to check the $returned_content, if it is a failure / error-code then send again the string via cURL to the SMS gateway.
if ($returned_content = 'ANY FAILURE / ERROR-CODE') {
//redo cURL
$link = "http://www.smsgateway.com/send.php?phoneno=".$phonenumber."&message=".urlencode($smsMessage);
$returned_content = using_cURL_function_to_get_response($link);
// Check the $returned_content
} else {
// inform me the '200 = SUCCESS' via email
mail($myEmail, $successfulSubjectt, $returned_content, $msgHeaders);
}
I'm not sure about this looping part, anyone could help?
I guess what you're trying to do is this:
$link = "http://www.smsgateway.com/send.php?phoneno=".$phonenumber."&message=".urlencode($smsMessage);
while ($returned_content != '200 = SUCCESS')
{
$returned_content = using_cURL_function_to_get_response($link);
}
mail($myEmail, $successfulSubjectt, $returned_content, $msgHeaders);
But it's not a good idea. Suppose you never do get a success? You will have an infinite loop. You should really rethink what you're trying to do.
So when you try to send an SMS and you get an error, you want to retry sending? How would that work?
Also, not sure, but the target server might interpret this as a DoS attack.
I think you should rethink your approach. If your goal is enabling some users to send SMS, then how about presenting them with a message saying that there was an error delivering their SMS and they should try later?
Related
About
I am trying to receive message posted on my server as soon as user post message the message in group or channel or direct in slack.
App Status
Code in the verified file where challenge was posted.
header('Content-type: application/json');
$myfile = fopen("test.txt", "w") or die("Unable to open file!");
$data = json_decode(file_get_contents('php://input'), true);
fwrite($myfile, $data["challenge"]);
fclose($myfile);
$json = '{"challenge":' . $data["challenge"] . '}';
echo json_encode(["challenge" => $json]);
Question
Now that the above url has been verified successfully, I am still not able to receive the posted messages. I was expecting messages posted at same url which was used to verify challenge parameter. Is that correct?
Am I missing anything retrieving the messages posted on my server?
Update - 1
Due to some reasons I am not even able to verify the url anymore. My server is not receiving any data. I am trying to save whatever is being posted my side but it is always blank everytime,
I think your assumptions are correct.
According to slack documentation you should be receiving posts with the given payload in this endpoint:
https://api.slack.com/apis/connections/events-api#the-events-api__receiving-events__events-dispatched-as-json
I'm still unsure how are you validating that this isn't the case though, if you kept the code above you would see no result of the post data sent by slack, also if you don't return a response status 200, slack will stop posting to this endpoint after 1 hour.
https://api.slack.com/apis/connections/events-api#the-events-api__responding-to-events
Can you try adding test.php file something like:
<?php
// get all the POST requests
if ($_SERVER["REQUEST_METHOD"] == "POST") {
file_put_contents('test.json', json_encode($_POST, JSON_PRETTY_PRINT), FILE_APPEND);
// return 200
http_response_code(200);
echo json_encode(["success" => true]);
return;
}
At the very beginning, validate the url again and check if the requests start being saved on test.json.
Alternatively can you check if you web server is routing POST requests to this url?
Try using postman to validate that
I'm writing a telegram robot,
This is my code and I want the user receive the message which they send into the bot:
telegram.php :
<?php
class telegram {
public $token;
public function __construct($token)
{
$this->token = $token;
}
public function recievedText()
{
$text= json_decode(file_get_contents('php://input'),true);
return $text;
}
public function sendMessage($userid,$text)
{
$url='https://api.telegram.org/bot'.$this->token.'/sendMessage?chat_id='.$userid.'&text='.$text;
file_get_contents($url) ;
}
}
?>
index.php :
<?php
require ('config.php');
require('telegram.php');
$telegram = new telegram(TOKEN);
$result = $telegram->recievedText();
$userid = $result->message->from->id;
$text = $result->message->text;
$telegram->sendMessage($userid,$text) ;
?>
config.php :
<?php
define('TOKEN','----');
?>
and I have already set webhook and checked the SSL of my site via this and it was set,
I don't know where is the problem that when I send the message to the bot it doesn't send back any message!
You checked if you have ssl, but you don't check if you really use it. You also don't check what is the response from the request you do
file_get_contents($url) ;
change this line to:
$requestResponse = file_get_contents($url);
var_dump($http_response_header);
var_dump($requestResponse);
to see if you succeed sending the request at first.
Without the response It's just guessing what could go wrong.
You should see response code = 200 if everything is OK if other number not beginning with 2xx then you fail sending the requrest.
Read about HTTPS connections with file_get_contents(), or try to use cURL. Hint: Just for the debugging you can skip SSL verification so at least this problem at the beginning is eliminated. But remember to turn the verification back on after debugging!
If you do the request as GET query you need to encode the text before putting it at the url - use: urlencode() for that.
$url='https://api.telegram.org/bot'.$this->token.'/sendMessage?chat_id='.$userid.'&text='.$text;
change to
$url='https://api.telegram.org/bot'.$this->token.'/sendMessage?chat_id='. urlencode($userid) . '&text='.urlencode($text);
or even better make use of http_build_query() and encode this way all variable besides $this->token. Given link has a plain example so I'll skip giving you code.
You need to set webhook before anything else. Fire a POST request using Postman to
https://api.telegram.org/bot/setWebhook
with Body parameter:
url: https://your-server/api-endpoint
drop_pending_updates : true (false) -> (optional) I use this to drop pending messages
i'm building a function in php to check if a user and password match with an external database, so i can extract the emailadres. They have created a soap with wsdl for me so i can check this.
I can already check if the user exists, but every time i try to send a SoapCall to request the email, i get the error message Caught exception:Internal Server Error.
I looked around for this error, and there is somehow something wrong with the array i send as a parameter during the request. The array consist of the login / password of the user i'm checking.
The weird thing is, if i only send the array with only one parameter, then i don't recieve an error, but then i only recieve an empty string in return cause the username and password don't match.
I used the same structure to check if the user exists, and that is not giving the error.
Here is my code to make the connection:
checkEmail('HMichiels','xxxxxxx');
function checkEmail($pLoginStr,$pPassWordStr){
try{
$client = new SoapClient('https://milliarium.gabo-mi.com/Forms/Generic/Security/ExternalEmailService.asmx?WSDL', array('trace'=>1));
$params = array('pLoginStr'=>$pLoginStr,
'pPassWordStr'=>$pPassWordStr,);
$response = $client->__soapCall("GetUserEmail",array($params));
$responseS = $response->GetUserEmailResult;
//check what i'm sending inside the soapcall
echo "REQUEST:\n" . $client->__getLastRequest() . "\n";
} catch(SoapFault $fault){
echo 'Caught exception:', $fault->getMessage();
}
Anyone with more experience knows what the problem is ? I have lost hours on this error ><
Tnx
Soap parameters are case sensitive and must match on the server side. You're sending pPassWordStr when you should be sending pPasswordStr, as defined in the WSDL.
i am trying to reverse geocode a bunch of addresses for my database. I have about 200 addresses, and half of them worked like a charm.. i did put the google maps request in a loop and it worked. There were about 100 adresses that returned a "INVALID REQUEST" message. I echoed the request url and put it in my browser and it returned a "Status: OK" and all the data. Why does my script return a invalid request and another browser-tab returns OK?
Thats the function i used:
function getLatLon($strasse, $plz) {
$inhalt = simplexml_load_file('http://maps.googleapis.com/maps/api/geocode/xml?address='.$strasse.',+'.$plz.'&sensor=true');
$geo['lat'] = $inhalt->result->geometry->location->lat;
$geo['lon'] = $inhalt->result->geometry->location->lng;
return $geo;
}
The request url in clear looks like this:
http://maps.googleapis.com/maps/api/geocode/xml?address=Dreik%C3%B6nigstra%C3%9Fe+12,+Erlangen&sensor=true
Would be awesome if someone could help me or detect an error :)
Regards,
Philipp
I am pretty sure the error is caused by the german characters you are sending unencoded. When you do it by browser the URL will be encoded automatically, but not automatically by your PHP-script.
Use rawurlencode for this :
$inhalt = simplexml_load_file('http://maps.googleapis.com/maps/api/geocode/xml?'.
'address='.rawurlencode($strasse).',+'.
rawurlencode($plz).'&sensor=true');
I've got a problem when checking for a response error after sending a Push Notification. This is my setup:
From my PHP server, I'm sending Push Notifications. These notifications are send in the enhanced format, so I can get an error response from the Apple server. For example: Error #7 "Invalid payload size".
The way I check for errors is reading the socket response:
const ERROR_RESPONSE_SIZE = 6;
$errorResponse = #fread($this->_apnsSocket, self::ERROR_RESPONSE_SIZE);
This works fine when there is an actual error. By my problem is: when there's no error, the "fread" call doesn't return anything and keeps loading forever.
Can anyone help me with this? Thanks for your help!
You need to set stream_set_blocking($this->_apnsSocket, 0); to 0 which is non-blocking mode, because on success Apple doesn't send back anything, but the fread is waiting for data in blocking mode.