2012-12-14 40 views
2
$adapter = new \Zend\Http\Client\Adapter\Curl(); 

    $client = new \Zend\Http\Client($url); 
    $client->setAdapter($adapter); 
    $client->setMethod('POST'); 
    $adapter->setOptions(array(
     'curloptions' => array(
          CURLOPT_POST => 1, 
      CURLOPT_POSTFIELDS => $data, 
      CURLOPT_HTTPAUTH => CURLAUTH_BASIC, 
      CURLOPT_USERPWD => "username:password", 
      CURLOPT_RETURNTRANSFER => 1, 
      CURLOPT_SSL_VERIFYPEER => FALSE, 
      CURLOPT_SSL_VERIFYHOST => FALSE, 

     ) 
    )); 
    $client->send(); 

上面有我使用什麼剪斷和我在這裏請遵照docs http://framework.zend.com/manual/2.0/en/modules/zend.http.client.adapters.htmlZF2:Zend的客戶端和捲曲不工作

的問題是,當我做我自己的捲曲函數調用它的工作原理罰款,我得到一個有效的迴應。但是當我使用zend會議時,我總是收到401未經授權的許可。我使用的捲曲選項對於這兩種方法都是相同的。

有什麼建議嗎?

回答

2

我記得與例子,所以我用另一個有問題,這個工作對我來說:

$request = new Request(); 
$request->setUri($url); 
$request->setMethod('POST'); 

$client = new Client(); 
$adapter = new \Zend\Http\Client\Adapter\Curl(); 
$client->setAdapter($adapter); 

$adapter->setOptions(array(
    'curloptions' => array(
     CURLOPT_POST => 1, 
     CURLOPT_POSTFIELDS => $data, 
     CURLOPT_HTTPAUTH => CURLAUTH_BASIC, 
     CURLOPT_USERPWD => "username:password", 
     CURLOPT_RETURNTRANSFER => 1, 
     CURLOPT_SSL_VERIFYPEER => FALSE, 
     CURLOPT_SSL_VERIFYHOST => FALSE, 
    ) 
)); 

$response = $client->dispatch($request); 
0

Zend公司不允許覆蓋選項CURLOPT_POSTFIELDS。

你需要弄清楚如何覆蓋它。

從curl.php的Zend庫中的invalidOverwritableCurlOptions中刪除此選項將解決您的問題。但是你需要找到不干擾圖書館的方式。

3

由於Curl.php中的invalidOverwritableCurlOptions數組阻塞了CURLOPT_POSTFIELDS和CURLOPT_POST,因此您想要在客戶端上設置參數。

在Zend的使用由setParameterPost方法/ HTTP /客戶端,像這樣:

use Zend\Http\Client; 
    use Zend\Http\Client\Adapter\Curl; 

    //some post params 
    $postParams = array('per_page' => 25); 

    $adapter = new Curl(); 
    // -- add your curl options here (excluding CURLOPT_POST and CURLOP_POSTFIELDS -- 

    $client = new Client('https://example.com/somePage'); 
    $client->setAdapter($adapter); 
    $client->setMethod('POST'); 
    $client->setParameterPost($postParams); 
    $response = $client->send($client->getRequest()); 

    //output the response 
    echo $response->getBody()."<br/>"; 

你甚至都不需要做一個Request對象作爲Client類將一個反正。在上面的代碼中,我只是使用getRequest方法檢索Client創建的那個,然後將其傳遞給send方法。