使用post将数据发送到Web服务

rfc*_*484 0 php json curl web-services

我已经获得了如何连接到某个网络服务器的示例.

这是一个带有两个输入的简单形式:

Web服务表格

在使用令牌和json提交表单后,它返回true.

这是代码:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Webservice JSON</title>
    </head>
    <body>
        <form action="http://foowebservice.com/post.wd" method="post">
            <p>
                <label for="from">token: </label>
                <input type="text" id="token" name="token"><br>
                <label for="json">json: </label>
                <input type="text" id="json" name="json"><br>
                <input type="submit" value="send">
                <input type="reset">
            </p>
        </form>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

为了使它动态化,我试图用PHP复制它.

$url = "http://foowebservice.com/post.wd";

$data = array(
  'token' => 'fooToken',
  'json' => '{"foo":"test"}',
);

$content = json_encode($data);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
  array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

$json_response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

curl_close($curl);

$response = json_decode($json_response, true);
Run Code Online (Sandbox Code Playgroud)

但我必须做错事,因为$响应它给出了错误的价值.

我不介意以任何其他方式这样做而不是使用Curl.

有什么建议?

UPDATE

正如在第一个答案中所建议的那样,我试图以这种方式设置$ data数组:

$data = array(
  'token' => 'fooToken',
  'json' => array('foo'=>'test'),
);
Run Code Online (Sandbox Code Playgroud)

然而,回应也是错误的.

我已经尝试过针对Chrome 的Postman REST - 客户端插件,并使用开发工具/网络,标题中的网址是:

Request URL:http://foowebservice.com/post.wd?token=fooToken&json={%22foo%22:%22test%22}
Run Code Online (Sandbox Code Playgroud)

我假设与使用CURL发送的URL相同.

Lor*_*con 5

您正在以JSON格式传递POST数据,尝试以k1 = v1&k2 = v2的形式传递它.例如,在$data数组定义后添加以下内容:

foreach($data as $key=>$value) { $content .= $key.'='.$value.'&'; }
Run Code Online (Sandbox Code Playgroud)

然后删除以下行:

$content = json_encode($data);
Run Code Online (Sandbox Code Playgroud)

curl_setopt($curl, CURLOPT_HTTPHEADER,
   array("Content-type: application/json"));
Run Code Online (Sandbox Code Playgroud)

完整代码(已测试):

test.php的

<?
$url = "http://localhost/testaction.php";

$data = array(
  'token' => 'fooToken',
  'json' => '{"foo":"test"}',
);

foreach($data as $key=>$value) { $content .= $key.'='.$value.'&'; }

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

$json_response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

curl_close($curl);

$response = json_decode($json_response, true);
var_dump($response);
?>
Run Code Online (Sandbox Code Playgroud)

testaction.php

<?
echo json_encode($_POST);
?>
Run Code Online (Sandbox Code Playgroud)

输出:

array(2) {
  'token' =>
  string(8) "fooToken"
  'json' =>
  string(14) "{"foo":"test"}"
}
Run Code Online (Sandbox Code Playgroud)