2013-10-24 29 views
2

我一直在爲此奮鬥了幾天 - 我需要發送一組以json編碼爲api的數據。我試圖使用Zend 2 http來實現這一點,但迄今爲止我還沒有運氣。以下是對API手冊上說:如何使用Zend 2 http發送json數據?

Bulk Create Contacts 
This command can be used to insert new numbers into your Textlocal contact groups 
and will allow you to insert extra fields such as the contact's name. 

Parameter Description 
group_id ID of the group you’d like to store the numbers in. This 
contacts A JSON object containing all the details for each contact. 

Note: It is recommended to send this request via POST. 

Sample Request 
(POST Variables): 
username: [email protected] 
hash: 4c0d2b951ffabd6f9a10489dc40fc356ec1d26d5 
group_id: 5 
contacts: [{"number":"447123456789","first_name":"Bob","last_name":"Smith","custom1":"","custom2":"","custom3":""},{"number":"447987654321","first_name":"Sally","last_name":"McKenzie","custom1":"","custom2":"","custom3":""},{"number":"447000000000","first_name":"Ramesh","last_name":"Dewani","custom1":"","custom2":"","custom3":""}] 

好了,所以這就是它預計,這是到目前爲止我的代碼: -

include 'Zend/Loader/StandardAutoloader.php'; 
$loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true)); 
$loader->register(); 

use Zend\Http\Client; 
use Zend\Http\Request; 

$data = array(
'username'  => 'myUsername', 
'hash' => 'myHash', 
'group_id'  => 123456, 
'contacts' => json_encode(array('number'=>447123456789,'first_name'=>'Bob','last_name'=>'Smith','custom1'=>"",'custom2'=>"","custom3"=>"")) 
); 


$uri = 'https://api.txtlocal.com/create_contacts_bulk'; 
$request = new Request(); 
$request->setUri($uri); 
$request->setMethod('POST'); 
$request->getPost()->set($data); 

    $config = array(
       'adapter' => 'Zend\Http\Client\Adapter\Socket', 
       'ssltransport' => 'ssl', 
       'sslcert' => '/etc/pki/tls/cert.pem', 
       'sslcapath' => '/etc/pki/tls/certs' 
       ); 

$client = new Client(null, $config); 
$client->setEncType(Client::ENC_FORMDATA); 
$response = $client->dispatch($request); 

if ($response->isSuccess()) { 
echo "the post worked!"; 
}else{ 
echo "the post failed"; 
} 

那不是工作,我敢肯定有我」中號失蹤 - 這裏是我的大火,劇本我得到的錯誤: -

[Thu Oct 24 09:29:47 2013] [error] [client] PHP Warning: Missing argument 2 for Zend\\Stdlib\\Parameters::set(), called in zend_test.php on line 24 and defined in Zend/Stdlib/Parameters.php on line 110 
[Thu Oct 24 09:29:47 2013] [error] [client] PHP Notice: Undefined variable: value in Zend/Stdlib/Parameters.php on line 112 
[Thu Oct 24 09:29:47 2013] [error] [client] PHP Fatal error: Uncaught exception 'Zend\\Http\\Client\\Exception\\RuntimeException' with message 'Cannot handle content type '' automatically' in Zend/Http/Client.php:1218\nStack trace:\n#0 Zend/Http/Client.php(858): Zend\\Http\\Client->prepareBody()\n#1 Zend/Http/Client.php(798): Zend\\Http\\Client->send(Object(Zend\\Http\\Request))\n#2 zend_test.php(30): Zend\\Http\\Client->dispatch(Object(Zend\\Http\\Request))\n#3 {main}\n thrown in Zend/Http/Client.php on line 1218 

你可以擺脫或幫助,你可以給任何光線我將不勝感激:)

在此先感謝

回答

8

您需要添加後的數據請求對象時設置ENC類型(這是不是在文檔很清楚)。另外,API手冊中的示例暗示,只有需要使用JSON的聯繫人才能編碼整個數組。

試試這個:

$data = array(
    'username' => 'myUsername', 
    'hash' => 'myHash', 
    'group_id' => 123456, 
    'contacts' => json_encode(array('number'=>447123456789,'first_name'=>'Bob','last_name'=>'Smith','custom1'=>"",'custom2'=>"","custom3"=>"")) 
); 

$request = new Request(); 
$request->setUri('https://api.txtlocal.com/create_contacts_bulk'); 
$request->setMethod('POST'); 
$request->getPost()->fromArray($data); 

$client = new Client(); 
$client->setEncType(Client::ENC_FORMDATA); 
$response = $client->dispatch($request); 

if ($response->isSuccess()) { 
    echo "the post worked!"; 
}else{ 
    echo "the post failed"; 
} 

編輯:爲了驗證SSL證書是有效的,HTTP客戶端需要你的Web服務器上的CA證書的路徑。

你在傳遞這個作爲一個配置選項,HTTP客戶端實例:

$client = new Client(null, array(
    'sslcapath' => '/etc/ssl/certs' 
)); 

這是在Ubuntu的路徑,你需要找出它是爲您的服務器。

+0

感謝 - 你是100%正確的,我錯過了事實,只有聯繫人需要編碼!我已經進入另一個錯誤代碼現在讀取: - [Thu Oct 24 11:33:59 2013] [error] [client] PHP致命錯誤:未捕獲的異常'ErrorException'與消息'stream_socket_enable_crypto()[function.stream-socket-enable-crypto]:SSL操作失敗,代碼爲1. OpenSSL錯誤消息:\ nerror:14090086:SSL例程:SSL3_GET_SERVER_CERTIFICATE:證書驗證失敗,位於/Zend/Http/Client/Adapter/Socket.php:276\nStack trace:\ n#0 [internal function ]: –

+0

不能適應所有的錯誤 - 我需要在我的末尾進行身份驗證嗎?我確定他們的服務器有一個有效的SSL,我知道我們的服務器。 –

+0

Curl需要您的服務器的CA路徑來驗證SSL證書。我已經更新了有關此信息的答案。 –

1

,而不是索取的長期結構順便說一句,響應和客戶端類取悅conider靜態客戶端用法:

http://framework.zend.com/manual/2.0/en/modules/zend.http.client-static.html

use Zend\Http\ClientStatic; 

// Simple GET request 
$response = ClientStatic::get('http://example.org'); 

// More complex GET request, specifying query string 'foo=bar' and adding a 
// custom header to request JSON data be returned (Accept: application/json) 
$response = ClientStatic::get(
    'http://example.org', 
    array('foo' => 'bar'), 
    array('Accept' => 'application/json') 
); 

// We can also do a POST request using the same format. Here we POST 
// login credentials (username/password) to a login page: 
$response = ClientStatic::post('https://example.org/login.php', array(
    'username' => 'foo', 
    'password' => 'bar', 
));