conflict for function copy() .

Discussion in 'PHP' started by bandito40, Mar 23, 2008.

  1. #1
    HI,

    I am using the copy() function to download a web page as shown in this
    example:

    $userAgent = $_SERVER['HTTP_USER_AGENT'];
    $context_options = array (
    'http' => array (
    'method' => 'GET',
    'header'=> "User-Agent: $userAgent"

    )
    );

    $context = stream_context_create($context_options);
    copy("http://www.example.com", "test/test.html", $context);

    However, contrary to the PHP document for the copy() function:

    bool copy ( string $source , string $dest [, resource $context ] )

    it returns the following error:

    Warning: Wrong parameter count for copy() in /var/www/printer/
    test2.php on line 17

    The copy() works fine except when the third parameter is included but,
    in this case it returns http code 403 because the User-Agent isn't
    specified. Is it possible to include the third parameter hence,
    resource $context as stated in the documentation, is this a bug, or is
    there something wrong with my code?

    I am using Ubuntu/Linux 7.04 and php 5.2.1.

    Thanks,

    Carl

    http://www.gaihosa.com
     
    bandito40, Mar 23, 2008 IP
  2. lawrenz

    lawrenz Peon

    Messages:
    246
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i think you cannot copy the whole site. hehehe try using file_get_contents function . =)
     
    lawrenz, Mar 24, 2008 IP
  3. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Hello,

    According to the PHP Docs:

    http://uk.php.net/copy

    As you are running 5.2.1, it does not have context support.

    Jay
     
    jayshah, Mar 24, 2008 IP
  4. bandito40

    bandito40 Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Actually, I think I found out why this is happening. I didn't add the line returns in the array. Something I over looked. In the mean I wrote some new code that uses fopen() and get_stream_context() to do the same thing. IT actually seamed to produce a few less timeouts and other connection errors.


    Thanks for the replies,


    Carl


    1.
    $context_options = array (
    2.
    'http' => array (
    3.
    'method' => 'GET',
    4.
    'header'=> "User-Agent: $userAgent \r\n"
    5.

    6.
    )
    7.
    );


    http://www.gaihosa.com
     
    bandito40, Mar 24, 2008 IP
  5. -NB-

    -NB- Peon

    Messages:
    153
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That's not what's causing the error. Read what jayshah said, you need to upgrade your PHP version. You have 5.2.1, context was added in 5.3.0. If you can't upgrade it or can't get your host to, drop the $context portion or use file_get_contents().
     
    -NB-, Mar 25, 2008 IP