在開發期間使用http_curl進行訪問參數;隨後在發送post請求中遇到了問題;
期間報錯:請使用json格式上傳數據;
請求體在上傳時早已經轉換爲了json格式;因爲跨域php無法正常發送json;
解決方法:我們使用http_curl解決,添加一行請求頭代碼;
curl_setopt($con, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($post_data)));
結果返回code:0;請求成功;
// 應用公共文件
function http_get($url, $post_data = null) {
$con = curl_init((string) $url);
curl_setopt($con, CURLOPT_HEADER, False);
curl_setopt($con, CURLOPT_SSL_VERIFYPEER, False);
curl_setopt($con, CURLOPT_RETURNTRANSFER, true);
if ($post_data != null) {
curl_setopt($con, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($con, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($post_data)));
}
curl_setopt($con, CURLOPT_TIMEOUT, 2);
$result = curl_exec($con);
return $result;
}