【问题标题】:Call Rest Api Using Zend\Http\Client Or Guzzle使用 Zend\Http\Client 或 Guzzle 调用 Rest Api
【发布时间】:2018-02-13 15:48:01
【问题描述】:

我在 laravel 中调用 magento2 API。使用 curl,我得到了正确的响应,但我想使用 GuzzleHttp\Client 或 Zend\Http\Client 调用 API。 如何使用这个调用我的 api,下面是我的 curl 代码 sn-p:

    $curl = curl_init();
    $data=array('username'=>$vendordata['merchant_name'],'password'=>$vendordata['password'],'customer'=>['email'=>$vendordata['email'],"firstname"=> $vendordata['firstname'], "lastname"=> $vendordata['lastname']],"addresses"=> ["region"=> $vendordata['address'], "region_id"=> 0],"default_shipping"=> true,"default_billing"=>true,"telephone"=>$vendordata['contact_number']);
    $postdata=json_encode($data);

    curl_setopt_array($curl, array(
        CURLOPT_URL => "http://10.10.10.7/magento2/rest/V1/customers",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => $postdata,
        CURLOPT_HTTPHEADER => array(
            "authorization: OAuth oauth_consumer_key=\"sganfgvr2naxwmh21jgi5ffijuci0207\",oauth_token=\"d16pdq1avr1rs7h9745rc0x6py65a2vt\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"1518006201\",oauth_nonce=\"4ghORA\",oauth_version=\"1.0\",oauth_signature=\"Ztq5ErznqvCl18GomWv0F55t5OA%3D\"",
            "cache-control: no-cache",
            "content-type: application/json",
            "postman-token: 5ec55151-3365-7ffc-a6a4-ce5fe5bc451f"
        ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo $response;
    }

【问题讨论】:

    标签: rest zend-framework zend-framework2 magento2 guzzle


    【解决方案1】:

    首先,打开终端并运行此命令安装 Guzzle

    composer require guzzlehttp/guzzle
    

    然后添加这些行以在特定控制器中包含 guzzle。

    use GuzzleHttp\Exception\GuzzleException;
    use GuzzleHttp\Client;
    

    现在,创建一个您要运行的方法来调用 API 并放入以下代码

    $client = new Client(); //GuzzleHttp\Client
    $result = $client->post('your-request-uri', [
        'form_params' => [
            'sample-form-data' => 'value'
        ]
    ]);
    

    就是这样!

    文档很好地解释了这一点:https://github.com/guzzle/guzzle

    【讨论】:

      【解决方案2】:

      这里是使用 guzzlehttp 代替 curl 时的完整代码

      $client = new GuzzleHttp\Client();
      $response = $client->request('POST', 'http://10.10.10.7/magento2/rest/V1/customers', array(
          headers' => [
              'Authorization' => 'OAuth oauth_consumer_key="sganfgvr2naxwmh21jgi5ffijuci0207",oauth_token="d16pdq1avr1rs7h9745rc0x6py65a2vt",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1518006201",oauth_nonce="4ghORA",oauth_version="1.0",oauth_signature="Ztq5ErznqvCl18GomWv0F55t5OA%3D',
              'Cache-Control' => 'no-cache',
              'Postman-Token' => '5ec55151-3365-7ffc-a6a4-ce5fe5bc451f'
          ],
          'json' => $postdata
      ));
      

      您还必须在您的 laravel 项目中为 PSR-7 support 安装以下依赖项,并查看此 stackoverflow question 作为参考:

      composer require symfony/psr-http-message-bridge
      composer require zendframework/zend-diactoros
      

      【讨论】:

      • 我收到此错误:400 Bad Request` 响应:{"message":"%fieldName 是必填字段。","parameters":{"fieldName":"customer"}}
      • 尝试使用form_params 而不是json,正如@TharinduLucky 下面建议的那样
      【解决方案3】:

      这是由于您没有在 HTTP 标头 上将Content-Type 定义为application/json。请试试这个

      $client = new GuzzleHttp\Client();
      $response = $client->request('POST', 'http://10.10.10.7/magento2/rest/V1/customers', array(
          headers' => [
              'Content-Type'  => 'application/json',
              'Authorization' => 'OAuth oauth_consumer_key="sganfgvr2naxwmh21jgi5ffijuci0207",oauth_token="d16pdq1avr1rs7h9745rc0x6py65a2vt",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1518006201",oauth_nonce="4ghORA",oauth_version="1.0",oauth_signature="Ztq5ErznqvCl18GomWv0F55t5OA%3D',
              'Cache-Control' => 'no-cache',
              'Postman-Token' => '5ec55151-3365-7ffc-a6a4-ce5fe5bc451f'
          ],
          'json' => $postdata
      ));
      

      别忘了使用 composer 加载依赖项

      composer require symfony/psr-http-message-bridge
      composer require zendframework/zend-diactoros
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-26
        • 2019-12-27
        • 2017-10-05
        相关资源
        最近更新 更多