我一直在爲此奮鬥了幾天 - 我需要發送一組以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
你可以擺脫或幫助,你可以給任何光線我將不勝感激:)
在此先感謝
喬
感謝 - 你是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 ]: –
不能適應所有的錯誤 - 我需要在我的末尾進行身份驗證嗎?我確定他們的服務器有一個有效的SSL,我知道我們的服務器。 –
Curl需要您的服務器的CA路徑來驗證SSL證書。我已經更新了有關此信息的答案。 –