Home > Article > Backend Development > Detailed explanation of the steps to send JSON format string in PHP based on CURL
This time I will bring you a detailed explanation of the steps for sending JSON format strings in PHP based on CURL Strings, what are the precautions for PHP to send JSON format strings based on CURL, the following is a practical case , let’s take a look.
/* * post 发送JSON 格式数据 * @param $url string URL * @param $data_string string 请求的具体内容 * @return array * code 状态码 * result 返回结果 */ function post_json_data($url, $data_string) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen($data_string)) ); ob_start(); curl_exec($ch); $return_content = ob_get_contents(); ob_end_clean(); $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); return array('code'=>$return_code, 'result'=>$return_content); } $arr = array('a'=>'555','b'=>56454564); dump(post_json_data('http://192.168.211.1/html/dump.php',json_encode($arr)));
Note: When receiving data, you do not use $_POST
, but
$content = file_get_contents('php://input');
I believe you have mastered the method after reading the case in this article. For more exciting content, please pay attention to other related articles on the php Chinese website!
Recommended reading:
ThinkPHP connection database operation case analysis
Detailed explanation of the steps to install the yaf extension in PHP7.1
The above is the detailed content of Detailed explanation of the steps to send JSON format string in PHP based on CURL. For more information, please follow other related articles on the PHP Chinese website!