简体   繁体   中英

php file_get_contents encoding issue

I am trying to Integrate an SMS service into my website. I need to make a HTTP call with a param named "msg" (urlencoded).

Currently, I am constructing the entire URL with msg param being urlencoded (ie $msg = urlencode($msg) ), and I am sending the SMS.

$msg = urlencode("Hello World");

Although what I receive on my phone is "Hello+World" and NOT "Hello World".

So is there an issue with file_get_contents ?

Also, is there anyway, I can see the string "file_get_contents" finally sends out ?

Try using rawurlencode() instead.

The principal difference (although there are others) is that it encodes spaces as %20 instead of + . This is quite likely the source of your problem.

I figured what was the issue:

I was using http_build_query to create the query string, and I was passing a urlencode 'd param to it. Hence it was being encoded twice.

So is there an issue with file_get_contents ?

No.

Also, is there any way, I can see the string file_get_contents finally sends out ?

Yes, with a network sniffer.

您需要在检索数据后使用urldecode()然后显示给用户

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM