我在Laravel 4中使用Guzzle从另一台服务器返回一些数据,但我无法处理Error 400错误请求
[status code] 400 [reason phrase] Bad Request
Run Code Online (Sandbox Code Playgroud)
使用:
$client->get('http://www.example.com/path/'.$path,
[
'allow_redirects' => true,
'timeout' => 2000
]);
Run Code Online (Sandbox Code Playgroud)
怎么解决?谢谢,
我正在尝试将远程文件(图像PNG,GIF,JPG ...)复制到我的服务器.我使用Guzzle因为我有时会使用copy()获得404,即使该文件存在且我还需要进行基本身份验证.此脚本位于由cron作业触发的命令中启动的长脚本中.我对Guzzle很新,我成功复制了图像,但我的文件有错误的mime类型.我一定是在做错事.请建议我这样做的好方法(包括检查复制和mime类型检查的成功/失败).如果文件没有mime类型,我会弹出一个包含详细信息的错误.
这是代码:
$remoteFilePath = 'http://example.com/path/to/file.jpg';
$localFilePath = '/home/www/path/to/file.jpg';
try {
$client = new Guzzle\Http\Client();
$response = $client->send($client->get($remoteFilePath)->setAuth('login', 'password'));
if ($response->getBody()->isReadable()) {
if ($response->getStatusCode()==200) {
// is this the proper way to retrieve mime type?
//$mime = array_shift(array_values($response->getHeaders()->get('Content-Type')));
file_put_contents ($localFilePath , $response->getBody()->getStream());
return true;
}
}
} catch (Exception $e) {
return $e->getMessage();
}
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我的mime类型设置为application/x-empty
看起来当状态不同于200时,Guzzle会自动抛出异常.如何阻止此行为并自行检查状态,以便我可以自定义错误消息?
编辑:这是针对Guzzle 3.X现在这是你如何使用Guzzle v 4.X(与Guzzle 6一起工作)
$client = new \GuzzleHttp\Client();
$client->get(
'http://path.to/remote.file',
[
'headers' => ['key'=>'value'],
'query' => ['param'=>'value'], …Run Code Online (Sandbox Code Playgroud) 我正在尝试为Symfony 2中的ReST客户端建立最佳实践模式,因为这对我们公司来说是一项非常常见的工作,我们在前端边缘使用Symfony应用程序与基于Java的后端通过HTTP/ReST进行交谈.
我的想法是,这些服务填补了DDD中针对特定域的"存储库"角色.根据Doctrine指定的约定,这些约定将返回返回Entity对象的Repository类.
我认为同样的约定也可以在这里工作,ReST客户端使用像Guzzle这样的库实现一个Repository类,或者只是直接使用Curl,无论如何,然后代码从XML或JSON执行基本的转换并返回到Entity上游开发人员操纵的对象.这与其他Symfony 2用例中的模式一致,从DDD的角度来看也是有意义的.
有没有人看到这个或更好的方法有问题?
我正在尝试构建一个端点,使用Slim PHP框架将传递给它的数据转发到API,但我无法从Guzzle请求中获取响应.
$app->map( '/api_call/:method', function( $method ) use( $app ){
$client = new GuzzleHttp\Client([
'base_url' => $app->config( 'api_base_url' ),
'defaults' => [
'query' => [ 'access_token' => 'foo' ],
]
]);
$request = $client->createRequest( $app->request->getMethod(), $method, [
'query' => $app->request->params()
]);
var_dump( $client->send( $request )->getBody() );
})->via( 'GET', 'POST', 'PUT', 'PATCH', 'DELETE' )->conditions( [ 'route' => '.+?' ] );`
Run Code Online (Sandbox Code Playgroud)
然后这给了我......
object(GuzzleHttp\Stream\Stream)[59]
private 'stream' => resource(72, stream)
private 'size' => null
private 'seekable' => boolean true
private 'readable' => boolean true …Run Code Online (Sandbox Code Playgroud) 有没有办法可以在发送之前或之后将完整请求打印出来作为字符串?
$res = (new GuzzleHttp\Client())->request('POST', 'https://endpoint.nz/test', [ 'form_params' => [ 'param1'=>1,'param2'=>2,'param3'=3 ] ] );
Run Code Online (Sandbox Code Playgroud)
如何将该请求视为字符串?(不是回复)
原因是,我的请求失败并返回403,我想知道究竟发送了什么; 因为使用PostMan时同样的请求.
我有以下两个功能
public function myEndpoint(){
$this->logger->debug('Started');
$this->guzzle->requestAsync('post', 'http://myurl.com/doNotWait')->wait();
$this->logger->debug("I shouldn't wait");
}
public function doNotWait(){
sleep(10);
$this->logger->debug("You shouldn't wait");
}
Run Code Online (Sandbox Code Playgroud)
现在我需要在日志中看到的是:
Started
I shouldn't wait
You shouldn't wait
Run Code Online (Sandbox Code Playgroud)
但我所看到的
Started
You shouldn't wait
I shouldn't wait
Run Code Online (Sandbox Code Playgroud)
我还尝试使用以下方法:
方式#1
public function myEndpoint(){
$this->logger->debug('Started');
$this->guzzle->requestAsync('post', 'http://myurl.com/doNotWait', ['synchronous' => false])->wait();
$this->logger->debug("I shouldn't wait");
}
Run Code Online (Sandbox Code Playgroud)
方式#2
public function myEndpoint(){
$this->logger->debug('Started');
$this->guzzle->requestAsync('post', 'http://myurl.com/doNotWait');
$queue = \GuzzleHttp\Promise\queue()->run();
$this->logger->debug("I shouldn't wait");
}
Run Code Online (Sandbox Code Playgroud)
但结果永远不是理想的结果.任何的想法?我正在使用Guzzle 6.x.
我正在使用Guzzle 6开发Laravel应用程序.许多功能依赖于API,我已经为其创建了一个包装器.
我的包装器是一个单独的类,它创建了Guzzle客户端__construct(),并具有各种公共函数,它们返回Guzzle请求的响应.
我正在使用的API每10秒限制40个请求.我正在缓存这些东西,所以达到这个限制是非常罕见的,但我想知道我的应用程序不会只会死掉它!
关于我的应用的一些说明:
所以,我的问题是,我应该如何确保我没有达到这个限制?我的一些想法如下:
HandlerStack直接狂饮.不确定这是否可行,但我之前使用过HandlerStack缓存响应.我试图不挑起过于自以为是的反应,但我确信这可能比上面的更好和/或更简单,或者如果它们是好主意,任何指针或建议都会很棒.
提前致谢.
我正在尝试使用guzzle 6,它运行正常,但是当涉及到如何记录所有api调用时我迷失了.我想简单地记录时间,从会话登录用户,网址以及与API调用有关的任何其他常见相关信息.我似乎找不到Guzzle 6引用的任何文档,只有guzzle 3(他们已经更改了日志记录addSubscriber调用).这是我当前的API调用方式:
$client = new GuzzleHttp\Client(['defaults' => ['verify' => false]]);
$res = $client->get($this->url . '/api/details', ['form_params' => ['file' => $file_id]]);
Run Code Online (Sandbox Code Playgroud) 我在PHP中使用Guzzle(v6.1.1)向服务器发出POST请求.它工作正常.我正在添加一些日志记录功能来记录发送和接收的内容,我无法弄清楚如何获取Guzzle发送到服务器的数据.我可以很好地得到响应,但是如何获取发送的数据?(这将是JSON字符串.)
这是我的代码的相关部分:
$client = new GuzzleHttp\Client(['base_uri' => $serviceUrlPayments ]);
try {
$response = $client->request('POST', 'Charge', [
'auth' => [$securenetId, $secureKey],
'json' => [ "amount" => $amount,
"paymentVaultToken" => array(
"customerId" => $customerId,
"paymentMethodId" => $token,
"publicKey" => $publicKey
),
"extendedInformation" => array(
"typeOfGoods" => $typeOfGoods,
"userDefinedFields" => $udfs,
"notes" => $Notes
),
'developerApplication'=> $developerApplication
]
]);
} catch (ServerErrorResponseException $e) {
echo (string) $e->getResponse()->getBody();
}
echo $response->getBody(); // THIS CORRECTLY SHOWS THE SERVER RESPONSE
echo $client->getBody(); // This …Run Code Online (Sandbox Code Playgroud) 我想用guzzle发送一个xml文件来执行帖子.我没有找到一个例子.
到目前为止我所做的是:
$xml2=simplexml_load_string($xml) or die("Error: Cannot create object");
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
$client = new Client();
//
$request = new Request('POST', $uri, [ 'body'=>$xml]);
$response = $client->send($request);
//
//$code = $response->getStatusCode(); // 200
//$reason = $response->getReasonPhrase(); // OK
//
echo $response->getBody();
Run Code Online (Sandbox Code Playgroud)
无论我尝试什么,我都会收到错误-1,这意味着xml无效.我发送的XML通过在线验证,有效%100
请帮忙.