dongpo4197 2019-05-21 13:12
浏览 91
已采纳

如何使用Curl发送空白接受标头 - PHP

I've seen a couple of related issues, but unfortunately, none of the recommended solutions had solved my problem.

I'm trying to send in a blank Accept header using PHP curl:

PHP Version: 7.3 Disto: Debian

    /**
     * @param string $method
     * @param string $url
     * @param string $body
     * @param array $headers
     *
     * @return Response
     */
    public function execute(string $method, string $url, string $body = '', array $headers = []): Response
    {
        $curlHeaders = [];
        $isAcceptHeaderPresent = false;
        $isExpectHeaderPresent = false;
        $curl = \curl_init();
        $response = new Response();
        foreach ($headers as $name => $value) {
            $curlHeaders[] = $value === '' ? "{$name};" : "{$name}: $value";

            $name = \strtolower($name);
            if ($name === 'accept') {
                $isAcceptHeaderPresent = true;
            }

            if ($name === 'expect') {
                $isAcceptHeaderPresent = true;
            }
        }

        if ($isAcceptHeaderPresent === false) {
            $curlHeaders[] = 'Accept: ';
        }

        if ($isExpectHeaderPresent === false) {
            $curlHeaders[] = 'Expect: ';
        }

        $options = [
            CURLOPT_URL => \str_replace(' ', '%20', $url),
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HEADER => false,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_AUTOREFERER => true,
            CURLOPT_CONNECTTIMEOUT => 120,
            CURLOPT_TIMEOUT => 120,
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_POSTFIELDS => $body,
            CURLOPT_HEADERFUNCTION => [$response, 'headerWrite'],
            CURLOPT_WRITEFUNCTION => [$response, 'bodyWrite'],
            CURLOPT_FILE => $response->getBodyAsResouce(),
            CURLOPT_HTTPHEADER => $curlHeaders,
        ];

        switch ($method) {
            case 'GET':
            case 'POST':
            case 'PATCH':
            case 'PUT':
            case 'HEAD':
            case 'DELETE':
            case 'CONNECT':
            case 'OPTIONS':
            case 'TRACE':
                $options[CURLOPT_CUSTOMREQUEST] = $method;
                break;
            default:
                throw new \Exception('Unsupported HTTP method received: ' . $method);
        }

        \curl_setopt_array($curl, $options);
        \curl_exec($curl);
        $response->setStatusCode(\curl_getinfo($curl)['http_code']);
        \curl_close($curl);

        return $response;
    }

If Accept: is provided, then no Accept header will be sent in what so ever.

Whereas Accept; make it so, that the blank value will be sent in along with */* value as well: "accept":"*/*,

Is there a way to send in just the Accept: header as is?

I wonder if this mechanism has been changed in the last couple of PHP and curl extension releases, given that none of the suggestions are working as intended.

  • 写回答

1条回答 默认 最新

  • dqrmkdu25623 2019-05-22 10:19
    关注

    Please see description of the issue in github.com/docker-library/php/issues/832

    Tldr; debian uses ancient 2.5y old curl package while other platforms are using newer versions. There must be a bug that's causing this.

    If you are on debian, and if you want to send a blank accept header using curl, you'll have to update the version to 7.64.0 from the (testing) repo.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#linux#的问题(输入输出错误):出现这个界面接着我重新装系统,又让修电脑的师傅帮我扫描硬盘(没有问题)用着用着又卡死(相关搜索:固态硬盘)
  • ¥15 cv::resize不同线程时间不同
  • ¥15 web课程,怎么做啊😭没好好听课 根本不知道怎么下手
  • ¥15 做一个关于单片机的比较难的代码,然后搞一个PPT进行解释
  • ¥15 python提取.csv文件中的链接会经常出现爬取失败
  • ¥15 数据结构中的数组地址问题
  • ¥15 maya的mel里,怎样先选择模型A,然后利用mel脚本自动选择有相同名字的模型B呢。
  • ¥15 Python题,根本不会啊
  • ¥15 会会信号与系统和python的来
  • ¥15 关于#python#的问题