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

LVL 1
tjyoungAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Become a CompTIA Certified 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.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.