php 使用 post 将数据发送到网络服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15834164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending data to a webservice using post
提问by rfc1484
I've been provided with an example of how to connect to a certain webserver.
我已经获得了如何连接到某个网络服务器的示例。
It's a simple form with two inputs:
这是一个带有两个输入的简单表单:
It returns true after submitting the form with both the token and the json.
提交带有令牌和 json 的表单后,它返回 true。
This is the code:
这是代码:
<!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>
In order to make it dynamic, I'm trying to replicate it using PHP.
为了使其动态化,我正在尝试使用 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);
But I must be doing something wrong, because $response it's given a false value.
但我一定是做错了什么,因为 $response 它被赋予了一个错误的值。
I don't mind doing this in any other way instead of using Curl.
我不介意以任何其他方式执行此操作而不是使用 Curl。
Any suggestions?
有什么建议?
UPDATE
更新
As suggested in the first answer, I've tried to set the $data array in this way:
正如第一个答案中所建议的,我尝试以这种方式设置 $data 数组:
$data = array(
'token' => 'fooToken',
'json' => array('foo'=>'test'),
);
However the response was also false.
然而,回应也是错误的。
I've tried the Postman REST - Client pluginfor Chrome, and using Development Tools / Network, the url in the headers is:
我已经尝试过Postman REST -Chrome客户端插件,并使用开发工具/网络,标题中的 url 是:
Request URL:http://foowebservice.com/post.wd?token=fooToken&json={%22foo%22:%22test%22}
Which I assume is the same url that should be sent using CURL.
我认为应该使用 CURL 发送相同的 url。
回答by Lorenzo Marcon
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:
您以 JSON 格式传递 POST 数据,尝试以 k1=v1&k2=v2 的形式传递它。例如,在$data
数组定义后添加以下内容:
foreach($data as $key=>$value) { $content .= $key.'='.$value.'&'; }
then remove the following lines:
然后删除以下几行:
$content = json_encode($data);
and
和
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
Complete code (tested):
完整代码(已测试):
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
测试操作.php
<?
echo json_encode($_POST);
?>
output:
输出:
array(2) {
'token' =>
string(8) "fooToken"
'json' =>
string(14) "{"foo":"test"}"
}
回答by dnagirl
Part of $data
is already json encoded. Try making $data
pure php. ie $data['json']=array('foo'=>'test');
一部分$data
已经是 json 编码的。尝试制作$data
纯 php。IE$data['json']=array('foo'=>'test');