2013-11-22 7 views
12

Per 2 giorni ho problemi con il mio script PHP sul mio server. Non ho cambiato nulla e all'improvviso non ha funzionato più.PHP - Tipo di contenuto non specificato supponendo applicazione/x-www-modulo-urlencoded

Ecco il codice:

$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); 

Ricevo questi messaggi di errore:

Avviso: file_get_contents(): Content-type non nominate assumendo/x-www-form applicazione -urlencoded in

Avviso: file_get_contents (https://mobile.dsbcontrol.de): impossibile aprire lo stream: richiesta HTTP non riuscita! Server interno HTTP/1.1 500 Errore in

Ma quando provo localmente lo script funziona perfettamente.

+0

Mi sono anche imbattuto in questo problema. Sembra che sia stato causato dall'aggiornamento dei pacchetti. Ho aggiunto: $ http ['header'] = 'Content-Type: application/json'. "\ R \ n"; che sopprime l'avviso. –

risposta

24

Si sta passando $contexts a file_get_contents() e che contiene solo l'intestazione User-Agent nell'array $opts. Tutte le altre intestazioni e opzioni sono nell'array $options che si aggiunge a $context ma non si sta utilizzando. Prova:

$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); 
+0

il primo errore è andato, grazie, ma ho ancora problemi con l'avvertimento. – hannsch

0

Mentre le risposte esistenti non ha funzionato per me, sono riuscito a risolvere il problema in questo modo:

The PHP Manual dice params deve essere un array associativo in formato $arr['parameter'] = $value. Fare riferimento ai parametri di contesto per un elenco di parametri standard del flusso.

 

    $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); 

Problemi correlati