使用post将数据发送到webservice [英] Sending data to a webservice using post
问题描述
我已获得如何连接到特定网络服务器的示例。
I've been provided with an example of how to connect to a certain webserver.
这是一个简单的表单,有两个输入:
It's a simple form with two inputs:
这是代码:
<!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>
为了使其动态,我试图使用PHP复制它。
In order to make it dynamic, I'm trying to replicate it using 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);
但我必须做错事,因为$ response给出了一个错误的值。
But I must be doing something wrong, because $response it's given a false value.
我不介意以任何其他方式这样做,而不是使用Curl。
I don't mind doing this in any other way instead of using Curl.
有任何建议吗?
UPDATE
如第一个答案所示,我已尝试设置$ data数组以这种方式:
As suggested in the first answer, I've tried to set the $data array in this way:
$data = array(
'token' => 'fooToken',
'json' => array('foo'=>'test'),
);
但是回应也是false。
However the response was also false.
我尝试了 Postman REST - 客户端插件 Chrome,并使用开发工具/网络,标题中的网址是:
I've tried the Postman REST - Client plugin for Chrome, and using Development Tools / Network, the url in the headers is:
Request URL:http://foowebservice.com/post.wd?token=fooToken&json={%22foo%22:%22test%22}
我假定是使用CURL发送的相同网址。
Which I assume is the same url that should be sent using CURL.
推荐答案
JSON格式的数据,尝试以k1 = v1& k2 = v2的形式传递它。
例如,在 $ data
数组定义后添加以下内容:
You're passing the POST data in JSON format, try to pass it in the form k1=v1&k2=v2.
For example, add the following after the $data
array definition:
foreach($data as $key=>$value) { $content .= $key.'='.$value.'&'; }
然后删除以下行:
$content = json_encode($data);
和
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
完成代码(测试):
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);
?>
testaction.php
<?
echo json_encode($_POST);
?>
输出:
array(2) {
'token' =>
string(8) "fooToken"
'json' =>
string(14) "{"foo":"test"}"
}
这篇关于使用post将数据发送到webservice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!