doumor942473 2017-11-07 16:22
浏览 50

Guzzle挂着两个apis沟通

in my application I have two laravel 5.2 servers running and they need to interact with each other to get resources using guzzle.

In my server 1, I use League Transformer to return some data, but the data needs to include a resource from my server 2 I get the data using guzzle in the transform function:

public function transform(MyModel $model) {

        $client = new Client();
        $result = $client->request('GET', 'localhost:8000/api/companies/'.$model->company_id);
        $company= $result->getBody()->getContents();

        $data = [
        'id' => (int) $model->id,
        'name' => $model->name,
        'status' => $model->status,
        ];

        $data['company'] = json_decode($company);
        return $data;
    }

When I make a request to get this resource to localhost:4040/api/models/1 I get this response:

"data": {
    "id": 1,
    "name": "John Doe",
    "status": "active",
    "company": {
        "data": {
            "id": 1,
            "name": "My Company",
        }
    }

The problem I'm facing is when I need to access MyModel (stored in server 1) from server 2 using guzzle, because I need to make a request to server 1:

$result = $client->request('GET', 'localhost:4040/api/models/'.$model->_id);

and server 1 needs to make a request to server 2 (in the transformer) to get the company. This makes the client to wait forever and never get a response. I tried using Async request, but I get an empty response, and if I force the promise to wait, it also hangs forever.

  • 写回答

1条回答 默认 最新

  • dongshi1102 2017-11-10 15:28
    关注

    Do you run your apps locally using PHP's integrated web server? If yes, then this is the issue.

    PHP's integrated web server can handle only one request simultaneously. You get a kind of deadlock on

    and server 1 needs to make a request to server 2

    Try to run both server 1 and 2 on Apache/nginx/...

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂