Guzzle中CakePHP HttpSocket的等价物
我有一段用CakePHP编写的代码,用于向URI发送HTTP Post请求并获取响应 我不知道CakePHP,需要将其更改为发送HTTP Post请求的样式 以下是CakePHP代码:Guzzle中CakePHP HttpSocket的等价物,php,cakephp,guzzle,Php,Cakephp,Guzzle,我有一段用CakePHP编写的代码,用于向URI发送HTTP Post请求并获取响应 我不知道CakePHP,需要将其更改为发送HTTP Post请求的样式 以下是CakePHP代码: private static function sendRequest() { App::uses('HttpSocket', 'Network/Http'); try { $HttpSocket = new HttpSocket();
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;
}
}
主要目标是:
如何使用Guzzle发送上述代码中的HTTP Post请求
任何帮助都将不胜感激
附言:
我用的是Guzzle 6.2
我相信它应该能工作,我还没有测试过,但想法是这样的。self::$requestData不是json!它是xml类型的。@HamedKamrava对不起,我的错,也许这一个对你有用,我已经编辑了代码。非常有用。非常感谢。
$client = new Client(['auth' => ['username', 'password'], 'headers' => ['Content-Type' => 'text/xml']]);
$client->request('POST', self::$webserviceUri, ['body' => self::$requestData]);