0

I have a script where I read a file using:

file_get_contents(urlencode($url));

I get this error:

failed to open stream: HTTP request failed! HTTP/1.0 400 Bad request

I tried this, but I still get the error. I've tried this:

ini_set('default_socket_timeout', 120);

This:

$opts = array('http'=>array('timeout' => 120));
         $context = stream_context_create($opts);
         $resul = file_get_contents($url,0,$context);

And this:

$opts = array('http'=>array('timeout' => 120,'header'=>'Connection : close'));
         $context = stream_context_create($opts);
         $resul = file_get_contents($url,false,$context);

Can you help me figure out why I get the error?

hunch_hunch
  • 2,283
  • 1
  • 21
  • 26
user3911183
  • 787
  • 1
  • 12
  • 34
  • 4
    `urlencode` should be called on the parameters as you're appending them to the URL, not on the URL as a whole. – Barmar Aug 18 '14 at 16:43
  • 2
    probably nothing you're doing with stream fiddling will help, because the url you're using is itself bad. e.g. a syntax error, bad parameter, etc... You can't just blindly urlencode the ENTIRE url, because that'll also encode necessary things. e.g. `http://example.com` becomes `http%3A%2F%2Fexample.com` and now it's no longer a proper url. – Marc B Aug 18 '14 at 16:44
  • `urlencode` is enconding the scheme and the host (`http%3A%2F%2Fexample%2Ecom`), returning a wrong URL. As @Barman said, just encode the parameters, not the URL as a whole. Also, use `rawurlencode`. – Ben Aug 18 '14 at 16:49
  • @Barmar just edited i did the urlencode for just the param but still getting the error , also in some case i don't even use urlencode and i have the same error...@Ben what's the rawurlencode ?? – user3911183 Aug 18 '14 at 16:54
  • @MarcB thanks just i'm trying to get the explaination ... need some time – user3911183 Aug 18 '14 at 16:54
  • ok now get the whole your idea thank u ... but i still not enable to solve the problem , cause i have some case when i'm using the file_get_contents without urlencode and i get the error .. why?? – user3911183 Aug 18 '14 at 16:58
  • What is the need to `urlencode`? What `default_socket_timeout` has to do with "http errors"? – Protomen Aug 18 '14 at 17:00
  • There could be some other problem with the URL. Does the same URL work when you use it in the browser? Also, some sites check the User-Agent, and reject requests with no agent. – Barmar Aug 18 '14 at 17:00
  • @Barmar yeah the same url ... – user3911183 Aug 18 '14 at 17:08
  • @GuilhermeNascimento i need urlencode for some url to pass array – user3911183 Aug 18 '14 at 17:08
  • you have to encode the query and not the url, show a working example of your problem. – Protomen Aug 18 '14 at 17:17
  • no i don't use a query , i'm supposed to use an existant file , so i have to prepare an array with the right input and to pass it as a get.here is a working example where i don't pass an array : http://essai.com/Essai/index.php?civ=Mr&nom=Combabessou&prenom=Gerome&cp=72210 – user3911183 Aug 18 '14 at 17:29
  • .. in this link i do pass array : "http://Essai.fr/essau/essai-ws.php?callback=&id=153&provenance=153&ret=a%3A2%3A%7Bs%3A5%3A%22infos%22%3Ba%3A10%3A%7Bs%3A8%3A%22civilite%22%3Bs%3A3%3A%22Mme%22%3Bs%3A5%3A%22lname%22%3Bs%3A0%3A%22%22%3Bs%3A5%3A%22fname%22%3Bs%3A8%3A%22Nathalie%22%3Bs%3A5%3A%22email%22%3Bs%3A17%3A%22tometnata%40free.fr%22%3Bs%3A3%3A%22tel%22%3Bs%3A0%3A%22%22%3Bs%3A7%3A%22adresse%22%3Bs%3A0%3A%22%22%3Bs%3A6%3A%22date_n%22%3Bs%3A10%3A%2201%2F06%2F1969%22%3Bs%3A2%3A%22cp%22%3Bs%3A0%3A%22%22%3Bs%3A5%3A%22" – user3911183 Aug 18 '14 at 17:29
  • the link i've pit doesn't exist , it's confidentiel sorry – user3911183 Aug 18 '14 at 17:30
  • @user3911183 Your array is inside the query (querystring), so what you do is extract the query, see my answer – Protomen Aug 18 '14 at 19:42

1 Answers1

1

You need encode only "querystring", extract query and enconding this, after append enconded query you "url".

Note: file_get_contents requires allow_url_fopen=On in "php.ini", try use curl

Example (read my comments in code)

Note: This example get error in connection and http errors

<?php
//Set your page example
$uri = 'http://localhost/path/webservice.php?callback=&id=153&provenance=153&ret=a:1:{s:5:"infos";a:8:{s:8:"civilite";s:3:"Mme";s:5:"lname";s:0:"";s:5:"fname";s:8:"Nathalie";s:5:"email";s:17:"tometnata@free.fr";s:3:"tel";s:0:"";s:7:"adresse";s:0:"";s:6:"date_n";s:14:"10:"01/06/1969";s:2:"cp";s:0:"";}}';

//extract url
$parsed_url = parse_url($uri);

//Create fixed url
$fixed_url = $parsed_url['scheme'] . '://' . $parsed_url['host'] . $parsed_url['path'];

//If exists query
if (isset($parsed_url['query'])) {
    $output = array();
    $result = array();

    //Extract querystring
    parse_str($parsed_url['query'], $output);

    //Encode values in querystring
    forEach($output as $k => $v) {
        $result[] = $k . '=' . rawurlencode($v);
    }

    //Append encoded querystring
    $fixed_url .= '?' . implode('&', $result);
}

echo 'GET url: ', $fixed_url, '<br>';

//Get result in page
$ch = curl_init();
$timeout = 30; //set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $fixed_url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$file_contents = curl_exec($ch);

$errornum = curl_errno($ch);

$info = curl_getinfo($ch);

$status = (int) $info['http_code'];

if ($errornum !== 0) {
    echo 'Error: ', curl_error($ch);
    $file_contents = NULL;
} else if ($status !== 200) {
    echo 'http_error: ', $status;
    $file_contents = NULL;
} else {
    echo 'Result:<hr>';
    echo $file_contents;
}

curl_close($ch);
?>

Enable curl

Community
  • 1
  • 1
Protomen
  • 9,471
  • 9
  • 57
  • 124
  • thanks and sorry for the late feedback , when i use curl_init() is gives me an error !! i guess it's a plugin which i should install ... am i right ? – user3911183 Aug 19 '14 at 08:04
  • working with the function you get me all this time , now i face a strange problem which is when i have array in my url ; the function stop working ... is that a solution for that ... thanks – user3911183 Dec 22 '14 at 17:33