gpt4 book ai didi

php - 如何使用 Zend 2 http 发送 json 数据?

转载 作者:行者123 更新时间:2023-12-02 06:02:01 26 4
gpt4 key购买 nike

几天来我一直在为此苦苦挣扎——我需要将一组以 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

如果您能提供任何帮助或帮助我,我们将不胜感激 :)

提前致谢

最佳答案

在向请求对象添加发布数据时需要设置 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 上的路径,您需要找出它适用于您的服务器。

关于php - 如何使用 Zend 2 http 发送 json 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19561117/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com