![](/img/trans.png)
[英]JSON Request works in Javascript but not in Nodejs (statusCode: 403)
[英]Json request works in javascript but not in php
我正在尝试使用 PHP 获取 JSON 对象。 当我用 Jquery 尝试它时它工作正常,但是当我用 PHP 尝试它时,它会返回一条超时消息。
查询代码:
$(document).ready(function(){
$.post("https://xxxxx/mig_search", {Keywords: 'test'}, function(result){
var myObj = JSON.parse(result);
$("body").html(result);
});
});
PHP代码:
$url = "https://xxxxx/mig_search";
$postdata = http_build_query(
array(
'Keywords' => 'teste',
)
);
$options = array('http' =>
array(
'method' => 'POST',
'header' => 'User-Agent: request',
'content' => $postdata,
));
$ctx = stream_context_create($options);
$result = file_get_contents($url, false, $ctx);
if (!empty($result)) {
echo $result;
} else {
echo "Nao funcionou!";
}
die;
试试下面的代码
$postdata = http_build_query(array ('Keywords' => 'teste'));
$options = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
. "Content-Length: " . strlen($postdata) . "\r\n",
'content' => $postdata
)
);
尝试添加一个 Content-Header:
'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
. "Content-Length: " . strlen($postdata) . "\r\n",
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.