簡體   English   中英

Json 請求在 javascript 中有效,但在 php 中無效

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM