发出post请求以获取php中的json数据
我试图用php向url发出post请求,该url以字符串形式返回json数据。 我的代码是:发出post请求以获取php中的json数据,php,http-post,Php,Http Post,我试图用php向url发出post请求,该url以字符串形式返回json数据。 我的代码是: $url = "http://mysite.xxx/getjson/"; $method = 'POST'; $request = array( 'http' => array( 'header' => "Content-type: application/html\r\nAccept: */*\r\n", 'method' => '
$url = "http://mysite.xxx/getjson/";
$method = 'POST';
$request = array(
'http' => array(
'header' => "Content-type: application/html\r\nAccept: */*\r\n",
'method' => 'POST',
'content' => http_build_query($params)
),
);
$context = stream_context_create($request);
$json = file_get_contents($url, false, $context);
echo $json;
我什么也得不到
我已经检查了url中的代码,我认为post参数没有被传递。
有人能帮我吗。
谢谢大家! 将其用作您的内容类型:
application/x-www-form-urlencoded
尝试使用cURL
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "http://mysite.xxx/getjson/",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $params,
CURLOPT_HTTPHEADER, array(
'Accept: application/json'
)
);
curl_setopt_array($ch, $curlConfig);
$result = json_decode(curl_exec($ch));
curl_close($ch);
print_r(result);
对不起,伙计们。当我使用其他端点代码进行测试时,代码正在运行。但我不明白,当我使用高级Rest客户端(chrome扩展)进行测试时,为什么我的第一个端点代码能够工作。我必须找到答案。无论如何,感谢guyz的回复您的变量中有什么$params我们无法确定问题不是源于提供JSON的服务器。我在$params中有数组('key1'=>'value1','key2'=>'value2')。我使用了高级REST客户端(chrome扩展)检查url中的代码,它工作正常。。我只需要发送post变量,因为“由于缺少post变量,我在url中的代码中收到了错误消息。我已尝试过,但收到了“HTTP请求失败!HTTP/1.1 406不可接受”警告完成,但仍然得到相同的警告服务器对其能够响应的内容挑剔。你能创建一个端点让我们自己测试吗?