Problems encountered in writing code (PHP does not receive the JSON data passed, PHP uses UTF8 usage)

Source: Internet
Author: User
Tags http post php server

Today, write code with Python processing data, to the PHP server, found that the data is not received, because it is cross-domain, so added the message header: Header (' access-control-allow-origin:* '), but there is no egg, and then continue to Baidu, Patchwork finally found a solution.

The scheme is as follows:

1. Send the data to add header: headers = {' Content-type ': ' Application/json '}

2. PHP receives data using: $GLOBALS [' Http_raw_post_data '].

Here's why: $_post: An array of variables passed by the HTTP POST method.  is an automatic global variable. PHP By default only recognizes the application/x-www.form-urlencoded standard data type, which is the post data that can be accepted normally.

Content such as text/xml or soap or application/octet-stream can not be resolved, if you use the $_post array to receive will fail! So keep the prototype and give it to $globals[' Http_raw_post_data ') to receive it.

PHP after receiving data, after uploading MySQL found Chinese garbled, add the header in PHP ("content-type:text/html; Charset=utf-8 "), MySQL set character sets Utf-8 also invalid, later found that PDO did not set the Chinese code, plus later, then succeeded.

Also attached

 How to use Utf-8 :

"UTF-8" is the standard notation, PHP is not case-sensitive in English, so it can also be written as "Utf-8". "UTF-8" can also be the middle of "-" omitted, written as "UTF8". General procedures can be identified, but there are exceptions (the following article), in order to be strict, it is best to use the standard capitalization "UTF-8".

Only use "UTF8" (MySQL) in the database can only use "UTF8" in the MySQL command mode, can not use "utf-8", that is, in the PHP program can only use "Set names UTF8 (no small bars)", if you add "-" This line command will not take effect, but the header in PHP to add "-", because IE does not know the "UTF8", for reasons see below.

Header in PHP:

<?php header (' content-type:text/html; Charset=utf-8 ');?>//Strange: Content-tyep with a colon, chatset is an equal sign.
Static file use:
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
Summary: "Utf-8" can only be used in MySQL with the alias "UTF8", but in other places use uppercase "UTF-8". 】

 

Problems encountered in writing code (PHP does not receive the JSON data passed, PHP uses UTF8 usage)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.