0

i've been answered in my question here :

File_get_contents($url): failed to open stream

, and since i work with that function which was great for performance , now i'm facing a strange problem which is when i have an array in my url , the function stops of working , is there any solution ?

here is an example of the url :

http://website.com/file.php?a=save&param1=90&param2=1330&paramS[399]=on&paramT[5]=Mme&Names[2]=Tantan&types[3]=Martine

updated : the function you gived me :

function curl_function($uri) {
    $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);
    }
    //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;
    }
    curl_close($ch);
    return $file_contents;
}

it doesn't work with the url i wroth above , but when i do a simple file_get_contents($url) it works fine ...

updated : The error i get :

( ! ) Warning: rawurlencode() expects parameter 1 to be string, array given in C:\wamp\www\pretty.php on line 24

Thanks.

Community
  • 1
  • 1
user3911183
  • 787
  • 1
  • 12
  • 34
  • Hi user3911183, which function exactly does not work? Could you explain how your code should work (edit the question please)? – Protomen Dec 22 '14 at 17:44
  • i did update my question. – user3911183 Dec 22 '14 at 17:46
  • 2
    Your link to the previous section suggests you're using curl. Please provide verbose debugging information as outlined in [an answer to Php - Debugging Curl](http://stackoverflow.com/a/14436877/367456). – hakre Dec 22 '14 at 17:47
  • @GuilhermeNascimento: I guess he means the function you suggested him here: http://stackoverflow.com/a/25370859/367456 - btw. what is it about the query string recomposition? why so much work for standard URLs like in this case? – hakre Dec 22 '14 at 17:51
  • But displays an error? "curl_exec" returns something? – Protomen Dec 22 '14 at 17:52
  • yeah , i've update my question with the error. – user3911183 Dec 22 '14 at 17:56
  • 1
    @user3911183: The problem is you arrays multiple levels deep in your query string, so you need a recursive functions in order to use `implode` and `rawurlencode` the string. But that just begs the question of why disassemble and reassemble it in the first place. – prodigitalson Dec 22 '14 at 18:19
  • how can i do that without affecting the functionality of the function for the other urls which are simple ? – user3911183 Dec 22 '14 at 18:23
  • @hakre, thanks great link, but the problem is not with the "curl". – Protomen Dec 22 '14 at 18:47
  • 1
    @user3911183: Use two functions: One is to fix the URI, one is to do the curl request. Then only fix those URLs which needs fixing. Then all correct URls use your transport function. Problem solved. – hakre Dec 22 '14 at 22:37

1 Answers1

1

Your problem is not with the CURL is that the "for loop" was getting a array and the "rawurlencode" does not work with "arrays", the best in case urls with "array" is to use http_build_query.

Example:

<?php

/**
 * rebuild URI by re-encoding query parameters
 */
function rebuild_uri($uri) 
{
    $parsed_url = parse_url($uri);
    $buffer     = $parsed_url['scheme'] . '://' . $parsed_url['host'] . $parsed_url['path'];

    if (isset($parsed_url['query'])) {
        parse_str($parsed_url['query'], $output);    
        $query = http_build_query($output, null, null, PHP_QUERY_RFC3986);
        $buffer .= '?' . $query;
    }

    return $buffer;
}

/**
 * Get result in page
 */
function curl_function($uri) 
{    
    $ch = curl_init($uri);
    $timeout = 30; //set to zero for no timeout
    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_code: ', $status;
        $file_contents = NULL;
    }
    curl_close($ch);

    return $file_contents;
}

$uri = rebuild_uri('http://localhost/?a[1]=1á&b[c]=1 as dasd&test[]=2&test[]=');
echo curl_function($uri);
hakre
  • 193,403
  • 52
  • 435
  • 836
Protomen
  • 9,471
  • 9
  • 57
  • 124