簡體   English   中英

PHP客戶端向服務器端發送請求並向遠程服務器發送服務器端請求

[英]PHP client post request to Server side and server side post request to remote server

我的方案如下:

  1. 單擊按鈕時,客戶端將向服務器端發送請求
  2. 一旦服務器端收到請求,它將向遠程服務器發送另一個請求以獲得結果
  3. 一旦響應到來,服務器端應該回應客戶端的響應。

客戶

$.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);

http://php.net/manual/en/function.curl-setopt.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM