简体   繁体   English

我想使用php作为代理服务器,将json数据传递到Web服务,我在做什么错?

[英]I would like to use php as proxy server to pass json data to a webservice, what am I doing wrong?

Below is php script I am trying to use as proxy to pass two parameters (username and password) to a web service to return data in json format. 以下是我试图用作代理的php脚本,用于将两个参数(用户名和密码)传递给Web服务,以返回json格式的数据。

This is a workaround to the famed same domain limitation policy. 这是著名的相同域限制策略的解决方法。

I keep getting invalid username or password. 我不断收到无效的用户名或密码。

The ajax file is trying to pass two parameters to the webservice via php proxy script. ajax文件正在尝试通过php代理脚本将两个参数传递给Web服务。

Here is a snippet of the ajax file: 这是ajax文件的片段:

var uname = $("#user").val();
var upass = $("#pass").val();

    $.post("proxyurl.php",
      { data: JSON.stringify({ LoginName: uname,Password: upass }) })
        .done(function(data) {

Below is proxyurl.php 下面是proxyurl.php

  <?php

    $ch = curl_init("http://myotherdomain.com/getUserDetails");
    $strUser = $_POST["uname"];
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt ($ch, CURLOPT_POSTFIELDS,"LoginName=".urlencode($strUser));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);      
    curl_close($ch);
    echo $output
  ?>

Thanks in advance for your assistance 提前感谢你的帮助

You're actually sending over LoginName and Password to your proxy script, not uname . 您实际上是通过LoginNamePassword发送到代理脚本的,而不是uname

As well, curlopt_Postfields is perfectly happy to accept an array of key/value pairs and will encode it for you. 同样,curlopt_Postfields非常乐意接受键/值对的数组,并将为您编码。 There is no need to manually building a key=value string yourself. 无需自己手动构建key=value字符串。

curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);

is technically all you'd really need to do. 从技术上讲,这是您真正需要做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM