在Guzzle中禁用URL编码

时间:2015-11-15 00:36:22

标签: php url guzzle

我一直在尝试在Guzzle中下载文件并且它是有线的,然后我注意到请求URL已经变得混乱。我不明白如何使用setEncodingType(false);功能。

这就是我现在所拥有的。

public class Foo{



    private $client;
    private $loginUrl = 'https://<site>/login';
    private $parseUrl = 'https://<site>/download';

    public function __construct()
    {
        require_once APPPATH . 'third_party/guzzle/autoloader.php';

        $this->client = new GuzzleHttp\Client(['cookies' => true, 'allow_redirects' => [
            'max' => 10,        // allow at most 10 redirects.
            'strict' => true,      // use "strict" RFC compliant redirects.
            'referer' => true,      // add a Referer header
            'protocols' => ['https'], // only allow https URLs
            'track_redirects' => true
        ]]);


    }



    public function download(){
       $q_params = array('param_a'=> 'a', 'param_b'=>'b');
       $target_file = APPPATH.'files/tmp.log';
       $response = $this->client->request('GET', $this->parseUrl,['query'=>$reportVars, 'sink' => $target_file]);

    }


}

有谁能告诉我如何在上面的代码中使用禁用url编码?

1 个答案:

答案 0 :(得分:0)

粗略浏览GuzzleHttp\Client::applyOptions的代码表示当您使用“查询”请求选项时,查询将构建到PHP_QUERY_RFC3986,如下所示:

    if (isset($options['query'])) {
        $value = $options['query'];
        if (is_array($value)) {
            $value = http_build_query($value, null, '&', PHP_QUERY_RFC3986);
        }
        if (!is_string($value)) {
            throw new \InvalidArgumentException('query must be a string or array');
        }
        $modify['query'] = $value;
        unset($options['query']);
    }

Guzzle内部使用GuzzleHttp\Psr7\Uri。请注意withoutQueryValue()withQueryValue()方法如何编码查询字符串。

我的查询参数“硬编码”有很多成功,如下所示:

$uri = 'http://somewebsite.com/page.html?param_a=1&param2=245';

我还要注意GuzzleHttp\Client内没有setEncodingType()。

相关问题