public function http_request( $url, $post = '', $timeout = 5 ){
if( empty( $url ) ){
return ;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
if( $post != '' && !empty( $post ) ){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($post)));
}
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
一開始我並沒有留意傳遞過來的數據是application/json編碼的json字符串,我在后台直接用接受application/x-www-form-urlencoded編碼格式的數據方式來取傳遞過來的數據(就是直接用的$_POST方式獲取的),結果當然沒什么也沒有取到了。后來,同事直接改了http_request()方法,直接傳遞application/x-www-form-urlencoded編碼格式的數據過來,我這就沒有做更改。
對於上面的問題,我一直納悶當時為什么沒有拿到傳遞過來的數據。今天項目基本完工,研究了以下。
<pphp中的curl()函數進行post請求的時候,傳遞數據的格式可以有以下幾種方式:
(1):由參數拼接而成的key=>value鍵值對字符串。形如以下: name=xxx&age=23$sex=1
這種請求參數默認是按照application/x-www-form-urlencoded進行編碼的。
(2):由參數組成的key=>value鍵值對數組(只能是一維數組,更高維度的數組會報錯)。形如以下格式:
[ name="xxx" , age = 23 , sex = 男 ]
這種請求參數默認是按照multipart/form-data格式進行編碼的。
上面說了,curl()進行post請求的時候,只能傳遞一維數組作為傳遞的參數,那么如果想要傳遞多維數組需要怎么處理那?有兩種方式可以來處理,分別是下面的方式3以及方式4。
(3):將多維數組進行http_build_query()進行處理,等到一個key=>value鍵值對格式的字符串。如下面所示:
$data = [ "msg"=>"這是一條測試數據", "xxx" => "yyyy", "msg_data" => [ "name"=>"sunms", "age"=>23, "sex"=>"男", "content"=>[ 1,2,3 ] ], ];
將得到以下的字符串:
msg=這是一條測試數據&xxx=yyyy&msg_data[name]=sunms&msg_data[age]=23&msg_data[sex]=男&msg_data[content][0]=1&msg_data[content][1]=2&msg_data[content][2]=3
這種方式也是通過application/x-www-form-urlencoded進行編碼的,在接收方可以通過$_POST直接獲取。
(4):將多維數組轉換為json格式的字符串,對字符串進行application/json格式編碼,在接收方通過file_get_contents(“php://input”)或者$GLOBALS[‘HTTP_RAW_POST_DATA’]的方式獲取傳遞過來的json格式的字符串,然后將json格式的字符串轉換為數組進行處理。 $data = [];
$data_string = json_encode($data);
.....
//設置header信息
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string))); 注意:對於application/json格式編碼的數據,$_POST是不能直接獲取的,需要通過file_get_contents(“php://input”)或者$GLOBALS[‘HTTP_RAW_POST_DATA’]的方式獲取。
For this tutorial, I am going to go through how to setup your development and production server environments properly for PHP development.
Development environment windows
Without having to install a Windows server, which is quite costly, you can install one of the several third-party applications to which will get your server environment up for you.
Some of these applications are XAMPP and WAMP. And all these third-party applications do the same thing―install Apache, MySQL, and PHP inside the program.
XAMPP is my personal favorite ― which you can download at www.apachefriends.org ― as it is cross-platform. XAMPP stands for cross-platform, Apache, MariaDB, PHP,Perl. When installing XAMPP, it is very straight forward (and just in case you need to know, you can install Laravel with XAMP ) . Just keep hitting next and agree to everything. However, for the development environment ,you need to install the development tools which just changes the php.ini file to allow errors message display. If error messages are not displayed, go to c:xampp/php/ and open the php.ini file, and make sure that everything under error handling and logging (line 463) has been turned on, then restart your Apache server.
