Goutte Post JSON或设置cookie

 阿凯宜_ 发布于 2023-01-29 13:07

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

我要么:

    在Goutte中手动设置cookie,

    或通过Goutte发送json帖子,以便更新cookie jar.

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

1 个回答
  • 这是一个类方法,可以作为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);
          }
      }
    }
    

    2023-01-29 13:08 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有