guzzle

GuzzleHttp Hangs When Using Localhost

◇◆丶佛笑我妖孽 提交于 2019-12-07 01:34:12
问题 Here is a simple code snipplet but this just hangs and unresponsive. $httpClient = new GuzzleHttp\Client(); // version 6.x $headers = ['X-API-KEY' => '123456']; $request = $httpClient->request('GET', 'http://localhost:8000/BlogApiV1/BlogApi/blogs/', $headers); $response = $client->send($request, ['timeout' => 2]); echo $request->getStatusCode(); echo $request->getHeader('content-type'); echo $request->getBody(); die(); Any pointers much appreciated. When I tried above with the github api

Setting up cookies for Guzzle CookieJar

烂漫一生 提交于 2019-12-07 01:32:40
问题 I am doing unit testing in PHP for a site that requires authentication. Authentication is cookie based, so I need to be able to put a cookie like this in the cookie jar: [ 'user_token' => '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae' ] The web application can then use this known good token for the testing data, and will be able to authenticate under testing conditions to interact with the data fixtures. Also, it must be a secure cookie, and I (obviously) need to set the

How to bypass Laravel Exception handling

扶醉桌前 提交于 2019-12-07 01:15:16
问题 I have a method that checks if a user has valid Session info. This is supposed to throw an Exception, Guzzle\Http\Exception\BadResponseException but when I try to catch it : catch (Guzzle\Http\Exception\BadResponseException $e) { return false; } return true Laravel doesn't get to this code and immediately starts it's own error handling. And ideas on how to bypass Laravels own implementation and use my own Catch. EDIT: I just found out Laravel uses the same Exception handler as Symfony, so I

Limit connecting time with Guzzle HTTP PHP client

时光怂恿深爱的人放手 提交于 2019-12-06 18:24:53
问题 I'm using Guzzle to open a list of url-s and get the headers. Some of the urls are taking too long to respond and can't be openned and i want to ignore them. It takes me up to 20+ seconds before Guzzle throws an exception and i want to change this and limit the time for connecting to 2 sec. I have this code but it still takes much longer: <?php include 'vendor/autoload.php'; $start = new \DateTime("now"); $start = $start->format("d.m.Y H:i:s"); echo $start."\n"; $client = new Guzzle\Http

Not getting expected response from Guzzle

非 Y 不嫁゛ 提交于 2019-12-06 17:12:38
问题 I'm trying to build an endpoint that forwards the data passed to it to an API using the Slim PHP Framework and I'm having trouble getting my response from a Guzzle request. $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

Sending multiple goutte requests asynchronously

南楼画角 提交于 2019-12-06 16:16:25
This is the code I am using require_once 'goutte.phar'; use Goutte\Client; $client = new Client(); for($i=0;$i<10;$i++){ $crawler = $client->request('GET', 'http://website.com'); echo '<p>'.$crawler->filterXpath('//meta[@property="og:description"]')->attr('content').'</p>'; echo '<p>'.$crawler->filter('title')->text().'</p>'; } This works but takes a lot of time to process? Is there any way to do it faster. For starters, there is nothing asynchronous about your code sample. Which means that your application will sequentially, perform a get request, wait for the response, parse the response and

How to use Guzzle on Codeigniter?

三世轮回 提交于 2019-12-06 07:22:18
I am creating a web application on Codeigniter 3.2 which works with the Facebook Graph API. In order to make GET & POST HTTP requests, I need a curl library for Codeigniter. I have found Guzzle but I Don't know how to use Guzzle on Codeigniter. Check this link: https://github.com/rohitbh09/codeigniter-guzzle $this->load->library('guzzle'); # guzzle client define $client = new GuzzleHttp\Client(); #This url define speific Target for guzzle $url = 'http://www.google.com'; #guzzle try { # guzzle post request example with form parameter $response = $client->request( 'POST', $url, [ 'form_params' =

关于重复发布文章的改进

大兔子大兔子 提交于 2019-12-06 05:12:05
同步文章使用Linux系统的Crond服务,通过命令 curl 请求服务器同步更新的url ,将发布时间 小于或等于 当前时间的文章发布到osc博客中去。 发现存同一篇文章多次重复同步的问题,推测是在网络不好的情况下,文章发布会超过一分钟,甚至更多,由于是定时任务是每分钟的执行,这时就可能产生多个crond, 根本原因是crontab使用的curl默认没有设定超时时间,一直等待请求响应,当网络突然好转时,会瞬间出现将多篇文章更新上去的情况。 目前的做法是限制curl的最大执行时间为45秒,理论上只要小于一分钟就可以了,不会同时出现多个crond进程就可以了。 然后对同步的url的action进行了加锁处理,使用的库是 texthtml/php-lock ,简单的文件锁就够用了,保证一篇文章只有一个请求同步的操作。同时对guzzle设置了timeout参数,时间在40秒。 来源: https://my.oschina.net/falcon10086/blog/3135828

How to prevent crashing when Guzzle detect 400 or 500 error?

守給你的承諾、 提交于 2019-12-06 04:53:21
问题 I'm using PHP guzzle I have tried public static function get($url) { $client = new Client(); try { $res = $client->request('GET',$url); $result = (string) $res->getBody(); $result = json_decode($result, true); return $result; } catch (GuzzleHttp\Exception\ClientException $e) { $response = $e->getResponse(); $responseBodyAsString = $response->getBody()->getContents(); } } I kept getting How to prevent crashing when Guzzle detecct 400 or 500 error ? I just want my application to continue

Sage One API - unsupported_grant_type

筅森魡賤 提交于 2019-12-06 04:32:31
问题 I am trying to obtain an access token for Sage One API by following the docs using Guzzle(v6) / Laravel 5.2 (Laravel's involvement is irrelevant for this question), it is stuck at the "request access token" stage. The error Client error: `POST https://api.sageone.com/oauth2/token` resulted in a `400 Bad Request` response: {"error":"unsupported_grant_type"} The offending code $client = new Client([ 'base_uri'=>'https://api.sageone.com', 'headers' => ['content_type' => 'application/x-www-form