ixudra / curl

Custom PHP curl library for the Laravel 5 framework - developed by Ixudra

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add warning in documentation as an argument for setting headers should be not key value array, but indexed array. Or make according changes in withHeader method

vasymus opened this issue · comments

After spending several hours on finding the bug, decide to use raw https://www.php.net/manual/ru/ref.curl.php

And found out that in the first time working with your library i just put headers wrong

Instead of correct way:

$response->withHeaders([
     'X-Requested-With: XMLHttpRequest',
    'Content-Type: application/x-www-form-urlencoded',
]);

i did wrong way:

$response->withHeaders([
     'X-Requested-With' => 'XMLHttpRequest',
    'Content-Type' => 'application/x-www-form-urlencoded',
]);

So, my proposition for you:
either put a warning in your documentation about headers (i know, you describe the right way, but maybe you should put some more highlighted description)
or you could rewrite you methods in vendor/ixudra/curl/src/Builder.php
public function withHeader
and
public function withHeaders

for example

    public function withHeader($header)
    {
        if (is_array($header)) $header = implode(": ", $header);
        $this->curlOptions[ 'HTTPHEADER' ][] = $header;
        return $this;
    }

and

    public function withHeaders(array $headers)
    {
        $h = [];
        foreach ($headers as $key => $value) {
            if (!is_numeric($key)) {
                $value = "$key: $value";
            }
            $h[] = $value;
        }
        $this->curlOptions[ 'HTTPHEADER' ] = array_merge(
            $this->curlOptions[ 'HTTPHEADER' ], $h
        );
        return $this;
    }

Hi @vasymus thanks for letting me know. An argument could be made for either case, of course, and I understand what you're saying. Since the point of this package is increased clarity and ease of use, I fully agree with your proposal. I'll make the necessary changes some time this weekend

Thanks @elimentz ! :-)

Now available in version 6.18.0! I only changed the withHeaders() method, as it seems very unlikely to me that the withHeader() method will be used incorrectly