【问题标题】:PHP client post request to Server side and server side post request to remote serverPHP客户端向服务器端发布请求,服务器端向远程服务器发布请求
【发布时间】:2016-02-27 18:21:54
【问题描述】:

我的场景如下:

  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);
}

我检查了登录服务器。它可以从远程服务器获取响应。但是如果我添加

$response = curl_exec($ch);
回调函数不起作用。

有人知道吗?

BR 达蒙

【问题讨论】:

    标签: php post curl


    【解决方案1】:

    这不起作用,因为您对数组进行了编码。

    $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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 2015-06-15
      • 1970-01-01
      • 2016-08-09
      • 2019-04-08
      • 2021-11-09
      相关资源
      最近更新 更多