3

I'm building a scrape script to extract some information from a website, and while testing it out I keep getting the following error:

PHP Warning: curl_setopt_array(): Array keys must be CURLOPT constants or equivalent integer values

It points to the following line in my code:

curl_setopt_array($ch, $curlOptions);

Now $ch is derived from:

$ch = curl_init($url);

and $url is set like this (the URL is deliberate as I'm working on handling errors generated by trying to open the website if it doesn't exist):

$url = "https://www.kjsdsajlksajksajdklsadajklda.com";

$curlOptions is set here:

    $curlOptions = array(
        'CURLOPT_RETURNTRANSFER' => true,
        'CURLOPT_HEADER'         => true,
        'CURLOPT_FOLLOWLOCATION' => true,
        'CURLOPT_ENCODING'       => "",
        'CURLOPT_AUTOREFERER'    => true,
        'CURLOPT_CONNECTTIMEOUT' => 120,
        'CURLOPT_TIMEOUT'        => 120,
        'CURLOPT_MAXREDIRS'      => 10,
        'CURLINFO_HEADER_OUT'    => true,
        'CURLOPT_SSL_VERIFYPEER' => false,
        'CURLOPT_HTTP_VERSION'   => 'CURL_HTTP_VERSION_1_1',
        'CURLOPT_COOKIE'         => $cookiesJar,
        'CURLOPT_USERAGENT'      => $userAgent,
    );

I've removed the $curlOptions block while testing, and when it's removed I don't get the error. However, commenting out the different elements of the array doesn't resolve the issue and the error persists. I've changed the URL I use as well and it doesn't make a difference. I have also changed any instance of true or false to 1 and 0 respectively and this hasn't made a difference either.

So I'm a bit stuck really. I'm developing this on a Debian 8 system that uses PHP 7, the version output is here:

PHP 7.0.16-1~dotdeb+8.1 (cli) ( NTS ) 
Copyright (c) 1997-2017 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.16-1~dotdeb+8.1, Copyright (c) 1999-2017, by Zend Technologies

What exactly is going on? Is there a compatibility issue with the code I've written (which should work with PHP 5) with PHP 7 or is there something more obvious or simple?

1
  • 2
    remove the quotes around your array keys , for example 'CURLOPT_RETURNTRANSFER' needs to be CURLOPT_RETURNTRANSFER Commented Mar 30, 2017 at 7:31

1 Answer 1

15

You use strings('CURLOPT_RETURNTRANSFER') instead of constants (CURLOPT_RETURNTRANSFER)

var_dump(CURLOPT_RETURNTRANSFER, 'CURLOPT_RETURNTRANSFER');
// output
int 19913
string 'CURLOPT_RETURNTRANSFER'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I got caught up in thinking the values were wrong. Didn't think the elements themselves were incorrect.

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.