【问题标题】:Consume a Rest API using Zend Framework 2使用 Zend Framework 2 使用 Rest API
【发布时间】:2017-10-13 11:32:01
【问题描述】:

我希望使用 Zend Framework 2 来使用 Rest API。

我试过了:

How to consume a Rest API using Zend Framework 2

$request = new Request();
$request->getHeaders()->addHeaders(array(
    'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'
));
$request->setUri($url);
$request->setMethod('POST');
$request->setPost(new Parameters(array('param1' => 'val1')));


$client = new Client($url, array(
    'sslverifypeer' => null,
    'sslallowselfsigned' => null,
));
$response = $client->dispatch($request);
$data = json_decode($response->getBody(), true);

谁能提供一个如何输入用户名和密码的例子:

我试过这个:

$request->getHeaders()->addHeaders(array(
    'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8',
    'user'      => 'username1',
    'password'  => 'password1'
));

【问题讨论】:

    标签: php rest zend-framework2


    【解决方案1】:

    【讨论】:

    • 这应该适用于基本 HTTP 身份验证 - 这就是您要登录的内容吗?
    【解决方案2】:

    它现在可以工作了,但是使用 curl :

    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => $url,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => "param1=value1",
      CURLOPT_HTTPHEADER => array(
        "cache-control: no-cache",
        "content-type: application/x-www-form-urlencoded",
        "password: password01",
        "user: user01"
      ),
    ));
    
    $response = curl_exec($curl);
    $err = curl_error($curl);
    
    curl_close($curl);
    
    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多