【问题标题】:Zend Http Client - Invalid Content Type ErrorZend Http 客户端 - 无效的内容类型错误
【发布时间】:2015-10-13 11:31:47
【问题描述】:

我在向 ApiGility API 发出 POST 请求时收到无效的内容类型错误。

array (size=4) 'type' => 字符串 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html' (长度=54)
'title' => string 'Unsupported Media Type' (length=22) 'status' => int 415 'detail' => string '指定的内容类型无效' (长度=30)

所以这告诉我我发送的内容类型不正确。

这是我的代码:

        $client = new Client(); //Zend/Http/Client
        $client->setUri('http://example.com/api/transfer');
        $client->setMethod('POST');
        $client->setOptions(
            [
                'maxredirects' => 0,
                'timeout' => 60
            ]
        );

        $client->setHeaders(['Accept' => 'application/json', 'Authorization' => 'Bearer 123453c112345c25256ff2dacb8ab212345ace91' ]);

        $client->setParameterPost(
            [
                'total' => 1,
                'code' => '0f08c43582f14686aabec4610b332629'
            ]
        );


        try {
            $response = $client->send();
        } catch (\Exception $e) {
            throw new \Exception($e);

        }

        $responseObject = json_decode($response->getBody());

        $hydrator = new \Zend\Stdlib\Hydrator\ObjectProperty;

        $result = $hydrator->extract($responseObject);

        die(var_dump($result));

我无法从手册中解决:http://framework.zend.com/manual/current/en/modules/zend.http.client.html 或实际的客户端代码,在哪里设置内容类型?

【问题讨论】:

    标签: php zend-framework laminas-api-tools


    【解决方案1】:

    'Content-Type' 应该只是用于$client->setHeaders() 的数组中的另一个条目。不过,客户端应该默认为'multipart/form-data'

    你能做一个var_dump()$client->getRequest()->getHeaders() 吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-02
      • 2013-04-04
      • 2020-07-25
      • 2011-07-17
      • 2017-11-14
      • 2012-12-19
      相关资源
      最近更新 更多