PHP基于curl模擬post提交json數(shù)據(jù)示例
2019-09-23 11:08愛(ài)代碼也愛(ài)生活 PHP教程
這篇文章主要介紹了PHP基于curl模擬post提交json數(shù)據(jù)操作,結(jié)合實(shí)例形式分析了php使用curl實(shí)現(xiàn)post方式提交json數(shù)據(jù)相關(guān)操作步驟與注意事項(xiàng),代碼簡(jiǎn)單實(shí)用,需要的朋友可以參考下
本文實(shí)例講述了PHP基于curl模擬post提交json數(shù)據(jù)。分享給大家供大家參考,具體如下:
這里php模擬post提交json數(shù)據(jù)操作的關(guān)鍵是在頭部設(shè)置Content-Type
02 | header( "Content-type:application/json;charset=utf-8" ); |
09 | $data = json_encode( $param ); |
10 | list( $return_code , $return_content ) = http_post_data( $url , $data ); |
11 | print_r( $return_content ); exit ; |
12 | function http_post_data( $url , $data_string ) { |
14 | curl_setopt( $ch , CURLOPT_POST, 1); |
15 | curl_setopt( $ch , CURLOPT_URL, $url ); |
16 | curl_setopt( $ch , CURLOPT_POSTFIELDS, $data_string ); |
17 | curl_setopt( $ch , CURLOPT_HTTPHEADER, array ( |
18 | "Content-Type: application/json; charset=utf-8" , |
19 | "Content-Length: " . strlen ( $data_string )) |
23 | $return_content = ob_get_contents(); |
25 | $return_code = curl_getinfo( $ch , CURLINFO_HTTP_CODE); |
26 | return array ( $return_code , $return_content ); |
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。