How does php write the json file sent by the client from the server?

Source: Internet
Author: User
Tags php write
Php: how to write the server to get the json sent from the client? the client uses the libcurl library to send a request to the server over http. how can I write a request not to use the socket PHP to receive the request?


Reply to discussion (solution)

You first
File_put_contents('test.txt ', 'get:'. print_r ($ _ get, 1). 'post: '. print_r ($ _ post, 1 ));
Check the content in test.txt.

If the http get method is used, the data is saved in the $ _ GET array.
If the http post method is used, the data is usually stored in the $ _ POST array.
Note: If the post method does not specify the data type, the submitted data can be obtained through $ HTTP_RAW_POST_DATA.

After finding the data, you can use the json_decode function to convert it to an array or object.

Direct output
Print_r ($ _ POST); ECHO "111 ";
Pint_r ($ _ GET );

You first
File_put_contents('test.txt ', 'get:'. print_r ($ _ get, 1). 'post: '. print_r ($ _ post, 1 ));
Check the content in test.txt.

Print_r ($ _ POST, 1) I did not see anything directly... The client sends a string.

Who are you looking for directly print_r ($ _ POST? To the client?
Does your client display the code for returning data?

So I want you to send print_r ($ _ POST, 1) to the file so that you can check it.

Get: Array
(
)
Post: Array
(
)
What I got is this, haha, or nothing... it hurts.

Who are you looking for directly print_r ($ _ POST? To the client?
Does your client display the code for returning data?

So I want you to send print_r ($ _ POST, 1) to the file so that you can check it.
Get: Array
(
)
Post: Array
(
)
What I got is this, haha, or nothing... it hurts.

Again
File_put_contents('test.txt ', file_get_contents ('php: // input '));
Look

Sorry, I am not helpful.

Again
File_put_contents('test.txt ', file_get_contents ('php: // input '));
Look

Nothing left ..

Are you sure the client sends data?

Are you sure you want to solve the decoding problem? my client uses c ++ libcurl post.

Are you sure the client sends data? Are you sure you want to solve the decoding problem? my client uses c ++ libcurl post.

Why did you not receive any data?

If possible, post the C ++ code snippet.

If possible, post the C ++ code snippet.

# Include "HHttp. h"
# Include "json. h"
// Import the curl header file
# Include "../libs/cocos2dx/platform/third_party/ios/curl. h"

Bool HHttp: pServer (){
//------------------------
CURL * curl; // initialize the CURL structure
CURLcode res;

Res = curl_global_init (CURL_GLOBAL_ALL); // Initialize all possible calls.
If (res! = CURLE_ OK)
{
Printf ("Failed to global init default [% d] \ n", res );
Return false;
}

// Curl = curl_easy_init ();
// Json: FastWriter writer;
// Json: Value root;
// Root ["action"] = "device ";
// Std: string json_file = writer. write (root );
If (curl)
{

Std: string cc;
Curl_easy_setopt (curl, CURLOPT_URL, "http: // 127.0.0.1/ceshi. php"); // request address
Curl_easy_setopt (curl, CURLOPT_POST, true); // enable POST submission
Curl_easy_setopt (curl, CURLOPT_POSTFIELDS, "account = zhycheng"); // send data
Curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L );
Curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, write_data); // The processed function
Curl_easy_setopt (curl, CURLOPT_WRITEDATA, & cc); // buffer memory
Res = curl_easy_perform (curl); // post
If (res! = CURLE_ OK ){
If (CURLE_OPERATION_TIMEDOUT = res) // connection failure timeout
{
CCMessageBox ("Please try again! "," Network connection timeout ");
CCLog ("Network Timeout: Error message: % s \ n", curl_easy_strerror (res ));
Return 0;
}
CCMessageBox ("Please try again! "," Network connection timeout ");
CCLog ("raw_send_recv: failed to start CURL, error message: % s \ n", curl_easy_strerror (res ));
Return 0;
}
CCLog ("~ Send Data Done ~~ ");
// Clear the curl structure and release resources.
Curl_easy_cleanup (curl );

} Else {
CCLog ("raw_send_recv: Failed to initialize CURL \ n ");
Return 0;
}
Return true;
}


Int allLength; // The total stream length.
Std: vector AllData; // total data

// This function is called when data is returned from the server.
Size_t HHttp: write_data (uint8_t * dataPtr, size_t size, size_t nmemb, void * stream ){

Int len = size * nmemb;

If (len! = 1440 ){
CCLog ("received server data, returned data length: % I", allLength );

} Else {
AllLength + = len;
AllData. push_back (dataPtr );
}
Size_t sizes = size * nmemb;
Return sizes;
}

Are you right?
Generally
// Initialize libcurl
CURLcode return_code;
Return_code = curl_global_init (CURL_GLOBAL_WIN32 );
If (CURLE_ OK! = Return_code)
Return 0;
CURL * curl_handle;
CURLcode res;
Curl_handle = curl_easy_init ();
Curl_easy_setopt (curl_handle, CURLOPT_URL, url );
....
You have
CURL * curl;
But no
Curl = curl_easy_init ();
Can the if (curl) branch be included?

Are you right?
Generally
// Initialize libcurl
CURLcode return_code;
Return_code = curl_global_init (CURL_GLOBAL_WIN32 );
If (CURLE_ OK! = Return_code)
Return 0;
CURL * curl_handle;
CURLcode res;
Curl_handle = curl_easy_init ();
Curl_easy_setopt (curl_handle, CURLOPT_URL, url );
....
You have
CURL * curl;
But no
Curl = curl_easy_init ();
Can the if (curl) branch be included?

The code does run. the connection is successful. what should I do?

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.