In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to use the CURLOPT_POSTFIELDS parameters of CURL in PHP". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In general, when we use CURL to submit POST data, we are used to writing this:
The copy code is as follows:
Curl_setopt ($ch, CURLOPT_POSTFIELDS,$post_data)
But this kind of writing doesn't work very well sometimes, and you may get 500 errors returned by the server. But when we try to submit data to the server using Socket, we get very correct results.
For example, we have the following PHP file on the server:
The copy code is as follows:
When we use CURL to send some data to the server without paying attention to details, we may get the following result, which is not our ideal result:
The copy code is as follows:
[CONTENT_TYPE] = > multipart/form-data; boundary=-f924413ea122
But if we use http_build_query ($post_data) instead of $post_data to submit data to this PHP script, we will get a different result, which is our ideal result:
The copy code is as follows:
[CONTENT_TYPE] = > application/x-www-form-urlencoded
As you can see from the above example, when you use CURL and the parameter is data, when you submit data to the server, the HTTP header sends Content_type: application/x-www-form-urlencoded. This is the header sent by the browser when a normal web page submits a form. And multipart/form-data, we know that this is the form for uploading files. Including the boundary delimiter, there will be a lot more bytes.
The official manual says:
The copy code is as follows:
The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like 'para1=val1¶2=val2&...' Or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.
When using arrays to provide post data, the CURL component is probably compatible with @ filename, which sets content_type to multipart/form-data by default. Although it has no impact on most servers, a small number of servers are not compatible.
After some summary, it is concluded that when there is no need to upload files, the data submitted by post should be processed by http_build_query as far as possible, and then sent out, which can achieve better compatibility and smaller request packets.
That's all for "how to use the CURLOPT_POSTFIELDS parameter of CURL in PHP". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.