[英]PHP client post request to Server side and server side post request to remote server
我的方案如下:
$.post('login_server.php'{act:"post",phone:phone,passwords:passwords},function(e){
alert(e);
},'json');
$act = isset($_POST["act"]) ? $_POST["act"] : "default";
if($act == "default"){
var_dump(123123);
}elseif($act == "post"){
$phone = $_POST["phone"];
$password = md5($_POST["passwords"]);
$data_array = array('phone' => $phone,'password' =>$password );
$jsonStr = json_encode($data_array);
$url = "http://xx.xx.xx.xx/xxxx/userinfo/login";
$data = http_post_json($url, $jsonStr);
echo json_encode(123);
}
function http_post_json($url, $jsonStr)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($jsonStr)
)
);
$response = curl_exec($ch);
$abc = json_encode($response);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return array($httpCode, $response);
}
我檢查了login_server。 它可以從遠程服務器獲取響應。 但如果我補充一下
$response = curl_exec($ch);回調函數不起作用。
有沒有人知道這個?
BRs達蒙
這不起作用,因為您編碼您的數組。
$jsonStr = json_encode($data_array);
試試吧
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_array);
要么
在這里你需要獲得'json'
並在你的頁面上解碼。
curl_setopt($ch, CURLOPT_POSTFIELDS, 'json='.$jsonStr);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.