2

私はこれに数日間苦労しています-jsonでエンコードされた一連のデータをAPIに送信する必要があります。これを達成するために Zend 2 http を使用しようとしていますが、これまでのところ運がありません。APIのマニュアルには次のように書かれています:

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: testing@Textlocal.co.uk 
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 

あなたが私に与えることができる、またはあなたが私に与えることができる助けがあれば、どんな光でも大歓迎です:)

前もって感謝します

ジョー

4

2 に答える 2

2

ところで、Request、Response、および Client クラスの長い構造の代わりに、静的な Client の使用法を検討してください。

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',
));
于 2014-04-14T01:33:00.663 に答える