We help IT Professionals succeed at work.

Encoding a url in php (not sure I'm doing it right, keeps failing)

tjyoung
tjyoung asked
on
Hi,
Below is some code that I use to send some data to twilio's api.
The last part: StatusCallback: a variable or two in the url (State, city and name) need to be encoded. I've tried a number of ways but not sure I'm doing it right. Twilio suggested:

"I think you should wrap the entire contents of the StatusCallback in the urlencode() function. As is, I believe the system is getting confused on what is a parameter of the overall request versus the parameters of your callback."

I can't figure out how to do it as suggested.
Any help?

 
$response = $client->request("/$ApiVersion/Accounts/$AccountSid/SMS/Messages",
            	"POST", array(
            	"To" => $FromCaller,
            	"From" => $textcalldisplay,
            	"Body" => $subscribemessage,
            	"StatusCallback" => "http://www.mydomain.ca/app/application/third_party/twilio/callin/text_complete.php?ContestId=$contestid&account_id=$account_id&CallerState=$CallerState&CallerCity=$CallerCity"
        	));

Open in new window

Comment
Watch Question

Most Valuable Expert 2011
Top Expert 2016

Commented:
You need to urlencode the keys and the data values, but not the entire query string.
Most Valuable Expert 2011
Top Expert 2016
Commented:
Looking at the example posted here, it looks like the keys do not have any encodable characters.   You can apply urlencode() to these variables, then create the request.

$FromCaller
$textcalldisplay
$subscribemessage
$contestid
$account_id
$CallerState
$CallerCity

Author

Commented:
This is what I've tried. Does it look right?

 
"StatusCallback" => "http://www.mydomain.ca/app/application/third_party/twilio/callin/text_complete.php?<?php echo urlencode(ContestId=$contestid&account_id=$account_id&CallerState=$CallerState&CallerCity=$CallerCity)?>"

Open in new window

"StatusCallback" => "http://www.mydomain.ca/app/application/third_party/twilio/callin/text_complete.php?<?php echo urlencode(\"ContestId=$contestid&account_id=$account_id&CallerState=$CallerState&CallerCity=$CallerCity\")?>"

Author

Commented:
I tried this but seems to be failing:

 
$FromCaller = urlencode($FromCaller);
			$textcalldisplay = urlencode($textcalldisplay);
			$subscribemessage = urlencode($subscribemessage);
			$contestid = urlencode($contestid);
			$account_id = urlencode($account_id);
			$CallerState = urlencode($CallerState);
			$CallerCity = urlencode($CallerCity);
   
			$response = $client->request("/$ApiVersion/Accounts/$AccountSid/SMS/Messages",
            	"POST", array(
            	"To" => $FromCaller,
            	"From" => $textcalldisplay,
            	"Body" => $subscribemessage,
            	"StatusCallback" => "http://www.mydomain.ca/app/application/third_party/twilio/callin/text_complete.php?ContestId=$contestid&account_id=$account_id&CallerState=$CallerState&CallerCity=$CallerCity&CallerName=$CallerName"
        	));

Open in new window

Author

Commented:
The API doesn't seem to fail, it just never sends out the text like above.

Author

Commented:
Hi media-tips,
That produces the same result as the previous suggestion, doesn't audibly fail, but never sends the text. I'm going to guess that the StatusCallback of the API simply doesn't handle it. There was a little uncertainty at their end how it would work.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Is this a synchronous call to the Twilio API, or could there be a post-back delay occurring?  The URLEncode() looks correct, so that is probably not the issue.  Have you looked in the Twilio error log?
Are you using Twilio's sandbox?

Did you checked the "StatusCallback" value?

Is it normal that you have "To" => $FromCaller and "From" => $textcalldisplay, it looks to me like you have inverted the values.

Author

Commented:
Hi,
The twilio error log shows no errors (though I'm guessing it never does when using the StatusCallback). There are no errors in my server log either.

If I was to pass only the contestid and the account id which doesn't require any urlecoding (they are just a single number for each value), then it goes through and the sms sent. Once I go and urlencode, it never sends.

*what I'm going to try is using a US number for SMS next since the Canadian SMS is currently in Beta in case that has something to do with it.

Author

Commented:
Not using the sandbox, real numbers.
The from display is all right. Bit of poor variable naming is all.
Trying US number now.

Author

Commented:
US number turns out the same. I guess contacting Twilio for  more details is about all I can do. As soon as I urlencode anything it dies silently never sending an SMS, no errors in twilio logs or server logs and no sign of an outgoing SMS in the twilio logs as well.
Good luck, let us know what the problem was in case you find a solution.
Most Valuable Expert 2011
Top Expert 2016

Commented:
As soon as I urlencode anything it dies silently...

Something is palpably amiss.  Can you try posting the before-and-after examples of the data and the urlencode(data) so we can see what's in there?

Author

Commented:
Hi,
In the end, Twilio replied back saying not to encode the variable names but just the values like:
ContestId=".urlencode($contestid)."&account_id=".urlencode($account_id)

Once I did that it worked perfectly. Go figure...
Thanks for all your help.

Author

Commented:
All the suggestions made sense and would have worked under normal circumstances with the final format being pretty much the same idea.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Interesting.  <?php echo urlencode("&account_id="); outputs %26account_id%3D which makes sense.  There was no encoding of the underscore.  In other words, urlencode() used correctly should have had no effect on the string "account_id".  Maybe I will experiment with it a little more.  Thanks for the points and the info from Twilio, ~Ray
Good news, and thanks for the points, ~Bernard