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.

10
  • Hi user3911183, which function exactly does not work? Could you explain how your code should work (edit the question please)? Commented Dec 22, 2014 at 17:44
  • i did update my question. Commented Dec 22, 2014 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. Commented Dec 22, 2014 at 17:47
  • 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. Commented Dec 22, 2014 at 18:19
  • 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. Commented Dec 22, 2014 at 22:37

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.