• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

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

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

0
tjyoung
Asked:
tjyoung
  • 9
  • 5
  • 4
2 Solutions
 
Ray PaseurCommented:
You need to urlencode the keys and the data values, but not the entire query string.
0
 
Ray PaseurCommented:
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
0
 
tjyoungAuthor 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

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
media-tipsCommented:
"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\")?>"
0
 
tjyoungAuthor 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

0
 
tjyoungAuthor Commented:
The API doesn't seem to fail, it just never sends out the text like above.
0
 
tjyoungAuthor 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.
0
 
Ray PaseurCommented:
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?
0
 
media-tipsCommented:
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.
0
 
tjyoungAuthor 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.
0
 
tjyoungAuthor Commented:
Not using the sandbox, real numbers.
The from display is all right. Bit of poor variable naming is all.
Trying US number now.
0
 
tjyoungAuthor 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.
0
 
media-tipsCommented:
Good luck, let us know what the problem was in case you find a solution.
0
 
Ray PaseurCommented:
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?
0
 
tjyoungAuthor 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.
0
 
tjyoungAuthor Commented:
All the suggestions made sense and would have worked under normal circumstances with the final format being pretty much the same idea.
0
 
Ray PaseurCommented:
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
0
 
media-tipsCommented:
Good news, and thanks for the points, ~Bernard
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 9
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now