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?

mickburkejnr
  • 3,652
  • 12
  • 76
  • 109
  • 2
    remove the quotes around your array keys , for example `'CURLOPT_RETURNTRANSFER'` needs to be `CURLOPT_RETURNTRANSFER` – hassan Mar 30 '17 at 07:31

1 Answers1

15

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

var_dump(CURLOPT_RETURNTRANSFER, 'CURLOPT_RETURNTRANSFER');
// output
int 19913
string 'CURLOPT_RETURNTRANSFER'
cetver
  • 11,279
  • 5
  • 36
  • 56