dongyong6332 2017-02-14 22:41
浏览 76
已采纳

Guzzle中的CakePHP HttpSocket的等价物

I have a piece of code which written in CakePHP for sending a HTTP Post Request to a URI and get response.

I don't know CakePHP and need to change that to Guzzle style for sending that HTTP Post Request.

Here is the CakePHP code :

private static function sendRequest() {
        App::uses('HttpSocket', 'Network/Http');
        try {
            $HttpSocket = new HttpSocket();
            return trim($HttpSocket->post(self::$webserviceUri, self::$requestData, [
                'header' => ['Content-Type' => 'text/xml'],
                'auth' => ['method' => 'Basic', 'user' => self::$username, 'pass' => self::$password]
            ]));
        } catch (SocketException $e) {
            CakeLog::write('request_error', 'Request Error: '. $e->getMessage());
            return false;
        }
    }

The Main Goal Is:

How can I send exactly a such HTTP Post Request( above code ) by using Guzzle?

Any helps would be great appreciated.

P.S: I'm using Guzzle 6.2

  • 写回答

1条回答 默认 最新

  • duanchandun1860 2017-02-14 22:51
    关注
    $client = new Client(['auth' => ['username', 'password'], 'headers' => ['Content-Type' => 'text/xml']]);
    $client->request('POST', self::$webserviceUri, ['body' => self::$requestData]); 
    

    I believe it should work, I haven't tested it out but the idea is this one.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改