簡體   English   中英

PHP - 假設應用程序/x-www-form-urlencoded 未指定內容類型

[英]PHP - Content-type not specified assuming application/x-www-form-urlencoded

兩天來,我的服務器上的 PHP 腳本出現問題。 我什么都沒改變,突然它不再起作用了。

這是代碼:

$query = http_build_query($data);
$options = array(
    'http' => array(
        'header' => "Content-Type: application/x-www-form-urlencoded\r\n".
                    "Content-Length: ".strlen($query)."\r\n",     
        'method'  => "POST",
        'content' => $query,
    ),
);
$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n",'method'  => 'POST',
        'content' => http_build_query($data),));
$contexts = stream_context_create($opts);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $contexts, -1, 40000);

我收到這些錯誤消息:

注意:file_get_contents():未指定內容類型,假設 application/x-www-form-urlencoded 在

警告:file_get_contents( https://mobile.dsbcontrol.de ):未能打開 stream:HTTP 請求失敗。 HTTP/1.1 500 內部服務器錯誤

但是當我在本地嘗試腳本時,它可以完美運行。

您正在將$contexts傳遞給file_get_contents() ,並且只包含$opts數組中的User-Agent標頭。 所有其他標頭和選項都在$options數組中,您添加到$context但未使用。 嘗試:

$query = http_build_query($data);
$options = array(
    'http' => array(
        'header' => "Content-Type: application/x-www-form-urlencoded\r\n".
                    "Content-Length: ".strlen($query)."\r\n".
                    "User-Agent:MyAgent/1.0\r\n",
        'method'  => "POST",
        'content' => $query,
    ),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context, -1, 40000);

雖然現有的答案對我不起作用,但我設法解決了這樣的問題:

PHP手冊params必須是$arr['parameter'] = $value格式的關聯數組。 有關標准流參數的列表,請參閱上下文參數。

$header = array(
            "Content-Type: application/x-www-form-urlencoded",
            "Content-Length: ".strlen($postdata)
        );


    $packet['method'] = "POST";
    $packet['header'] = implode("\r\n", $header);
    $packet['content'] = $postdata;

    $transmit_data = array('http' => $packet);
    $context = stream_context_create($transmit_data);

我在用這個

$url = '';

$result = json_decode(file_get_contents($url, false, stream_context_create(array(
            'http' => array(
                'method'  => 'POST',
                'header' => 'Content-type:application/x-www-form-urlencoded',
                'content' => http_build_query($dataQuery)
            )
        ))), true);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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