Goutte Post JSON或设置cookie

sta*_*e76 0 html php symfony goutte

我正在尝试使用Goutte提交表单.该表单使用jQuery将表单序列化为json并发布到url.提交后,它会更新浏览器的cookie.

我要么:

  1. 在Goutte中手动设置cookie,
  2. 或通过Goutte发送json帖子,以便更新cookie jar.

我尝试使用Goutte的addContent方法创建一个表单然后发布它,但它不是作为JSON发送的,只是一个常规的查询字符串.

tac*_*one 6

这是一个类方法,可以作为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);
      }
  }
}
Run Code Online (Sandbox Code Playgroud)