Assign new items in an array without rewriting the whole code

Asked

Viewed 41 times

2

Is there any way to add new items without rewriting the code ?

See: If there are cookies then I use CURLOPT_COOKIE If not, I don’t use:

if ($cookies) {
            $options = [
                CURLOPT_URL             => $url,
                CURLOPT_CUSTOMREQUEST   => 'GET',
                CURLOPT_HEADER          => true,
                CURLOPT_RETURNTRANSFER  => true,
                CURLOPT_SSL_VERIFYPEER  => false,
                CURLOPT_SSL_VERIFYHOST  => false,
                CURLOPT_COOKIE          => implode('; ', $cookies),
                CURLOPT_USERAGENT       => $this->ua
            ];
        } else {
            $options = [
                CURLOPT_URL             => $url,
                CURLOPT_CUSTOMREQUEST   => 'GET',
                CURLOPT_HEADER          => true,
                CURLOPT_RETURNTRANSFER  => true,
                CURLOPT_SSL_VERIFYPEER  => false,
                CURLOPT_SSL_VERIFYHOST  => false,
                CURLOPT_USERAGENT       => $this->ua
            ];
        }
  • Add new items to the $options array?

  • That’s right. @Taffarelxavier

3 answers

1


If that’s what I understand, which is to add data to the array, you can:

//Adiciona um valor ao array com a chave `CURLOPT_OTHER`
 $options['CURLOPT_OTHER'] = 'OUTRO VALOR';

If you want to add as push, do the following:

$options[] = 'OUTRO VALOR';

To make it more readable, you can do the following:


$url = "myUrl";

$options = [
                CURLOPT_URL             => $url,
                CURLOPT_CUSTOMREQUEST   => 'GET',
                CURLOPT_HEADER          => true,
                CURLOPT_RETURNTRANSFER  => true,
                CURLOPT_SSL_VERIFYPEER  => false,
                CURLOPT_SSL_VERIFYHOST  => false,
                CURLOPT_USERAGENT       => "agent"
            ];
//Se existir, então adiciona ao array a chave `CURLOPT_COOKIE` com o valor $cookie
if ($_COOKIE) {
      $options[CURLOPT_COOKIE] = implode('; ', $_COOKIE);
}

var_dump($options);

REFERENCE:

https://www.php.net/manual/en/language.types.array.php

  • Created two arrays at the end of the array looked like this: 19914 => 
 array (size=1)
 10022 => string it’s normal that ?

  • Actually, I showed you two ways to add data to an array. The famous push.

  • View the issue...

  • Why did you use the predefined constants of curl? https://www.php.net/manual/en/curl.constants.php. They return numbers, so 19914, 10022; for example the constant CURLOPT_URL returns 10002.

  • Exactly, they return numbers, but never ordered... but it was solved thanks

1

Consider always having the key, but with an empty value if there are no cookies. I don’t know if I would use Curl constants to index the array, but I’ll leave it as you did. It would look like this:

$options = [
    CURLOPT_URL             => $url,
    CURLOPT_CUSTOMREQUEST   => 'GET',
    CURLOPT_HEADER          => true,
    CURLOPT_RETURNTRANSFER  => true,
    CURLOPT_SSL_VERIFYPEER  => false,
    CURLOPT_SSL_VERIFYHOST  => false,
    CURLOPT_USERAGENT       => "agent",
    CURLOPT_COOKIE          => empty($cookies) ? null : implode('; ', $cookies)
];
  • You’d do it that way ?

  • Maybe even in your own way. More important than the chosen way is to understand the difference between using CURLOPT_COOKIE and 'CURLOPT_COOKIE' as key. The first is actually a constant that contains a number. The second is a string.

0

You can use the merge array function for this, as follows:

$options = [
    CURLOPT_URL             => $url,
    CURLOPT_CUSTOMREQUEST   => 'GET',
    CURLOPT_HEADER          => true,
    CURLOPT_RETURNTRANSFER  => true,
    CURLOPT_SSL_VERIFYPEER  => false,
    CURLOPT_SSL_VERIFYHOST  => false,
    CURLOPT_USERAGENT       => $this->ua
];

if ($cookies) {
    $options = array_merge($options, [
            CURLOPT_COOKIE =>implode('; ', $cookies)
        ]
    )
}

This way it will add only the cookie parameter to the options array

  • It didn’t work. It turned the value of the constant code into an ordered tile

  • $options[CURLOPT_COOKIE] = implode... within the if would suffice, no?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.