如何使用Guzzlehttp / guzzle 6发送Cookies?

时间:2016-12-06 15:13:57

标签: php cookies guzzle

我需要发送带有自定义Cookie的请求。

我还有一些麻烦,我不知道怎么做......

谢谢!

4 个答案:

答案 0 :(得分:27)

error: can't exec 'copypng' (No such file or directory)
Command copypng failed with exit code 71

您可以阅读文档here

答案 1 :(得分:4)

如果使用cookie请求选项,

食尸鬼可以为您维护cookie会话。发送请求时,必须将Cookies选项设置为GuzzleHttp\Cookie\CookieJarInterface的实例。

// Use a specific cookie jar
$jar = new \GuzzleHttp\Cookie\CookieJar;
$r = $client->request('GET', 'http://httpbin.org/cookies', [
    'cookies' => $jar
]);

如果您想对所有请求使用共享的cookie jar,可以在客户端构造函数中将cookie设置为true。

// Use a shared client cookie jar
$client = new \GuzzleHttp\Client(['cookies' => true]);
$r = $client->request('GET', 'http://httpbin.org/cookies');

也检查完整的quickstart

答案 2 :(得分:3)

使用Guzzle向请求中添加Cookie的另一种方法:

function Edit({ history, match }) {
const { id } = match.params;
const isAdd = !id;

const initialValues = {

    firstName: '',
    lastName: '',
 
};

const validationSchema = Yup.object().shape({

    firstName: Yup.string()
        .required('First Name is required'),
    lastName: Yup.string()
        .required('Last Name is required'),
});


return (
    <Formik initialValues={initialValues} validationSchema={validationSchema} onSubmit={()={}}>
        {({ errors, touched, isSubmitting, setFieldValue }) => {
            useEffect(() => {
                if (!isAdd) {
                    getById(id).then(user => {
                        const fields = ['firstName', 'lastName'];
                        fields.forEach(field => setFieldValue(field, user[field], false));
                    });
                }
            }, []);

            return (
                <Form>
        
                        <div className="form-group col-5">
                            <label>First Name</label>
                            <Field name="firstName" type="text" className={'form-control' + 
                           (errors.firstName && touched.firstName ? ' is-invalid' : '')} />
                            <ErrorMessage name="firstName" component="div" className="invalid- 
                               feedback" />
                        </div>
                        <div className="form-group col-5">
                            <label>Last Name</label>
                            <Field name="lastName" type="text" className={'form-control' + 
                              (errors.lastName && touched.lastName ? ' is-invalid' : '')} />
                            <ErrorMessage name="lastName" component="div" className="invalid- 
                               feedback" />
                        </div>
                    </div>
             </Form>
            );
        }}
    </Formik 

答案 3 :(得分:1)

对于在laravel中使用Guzzle Http发送cookie,您可以使用以下示例代码:

        //your address
        $address = "http://example.com/xyz";
        
        //your cookie
        $coockie = ['Cookie' => "Key=Value"];
        
        //your request
        $res = Http::withOptions([
            'headers' => $coockie
        ])->get($address);