我正试着用Goutte提交一份表格。该表单使用jQuery将表单序列化为json,并将其发布到url。提交后,它将更新浏览器的cookie。
我要么需要:
我尝试使用Goutte的addContent方法创建一个表单,然后发布它,但是它不是以JSON的形式发送的,只是一个常规的查询字符串。
发布于 2014-02-04 22:36:40
下面是一个类方法,它可以作为Goutte的JSON和URLencoding的示例。
use Goutte\Client;
class a
{
/**
* abstract the request content-type behind one single method,
* since Goutte does not natively support this
* @param string $method HTTP method (GET/POST/PUT..)
* @param string $url URL to load
* @param string $parameters HTTP parameters (POST, etc) to be sent URLencoded
* or in JSON format.
* @return \Symfony\Component\BrowserKit\Response
*/
protected function makeRequest($method, $url, $parameters) {
$client = new Client();
if ($this->requestFormat == 'json') {
return $client->request($method, $url, array(), array(), array('HTTP_CONTENT_TYPE' => 'application/json'), json_encode($parameters));
} else {
return $client->request($method, $url, $parameters);
}
}
}发布于 2014-02-04 22:30:30
要在Goutte中设置cookie,请尝试如下:
$client->getCookieJar()->set($cookie);我相信$cookie是Symfony\Component\BrowserKit\Cookie类型的,因此如果您检查文档,您应该能够以调用所需的形式发送cookie。
如果希望通过POST加载资源,请尝试如下:
$client->request('post', $url, $params);我希望$params是提交给JSON操作的值的关联数组。
https://stackoverflow.com/questions/21564800
复制相似问题