使用 ZendFramework2 请求 Random.org API
Request to Random.org API with ZendFramework2
我是 ZF2 的新手,我想创建一些利用外部 API 的应用程序。我已经成功创建了向 random.org API 发送请求的方法,但作为响应,它向我发送了一个与解析相关的错误。我不知道我做错了什么,也不知道如何改进我的方法以获得正确的响应数据。
这是我用来发送请求的方法(我有意更改了 api 密钥,在我的应用程序中我使用了正确的密钥):
public function makeRequest()
{
$data = array(
'jsonrpc' => '2.0',
'method' => 'generateIntegers',
'params' => array(
'apiKey' => '00000000-0000-0000-0000-000000000000',
'n' => 10,
'min' => 1,
'max' => 10,
'replacement' => true,
"base" => 10),
'id' => 23866,
);
$client = new Client('https://api.random.org/json-rpc/1/invoke', array(
'adapter' => 'Zend\Http\Client\Adapter\Curl'
));
$client->setEncType(Client::ENC_FORMDATA);
$request = new Request();
$request->setUri('https://api.random.org/json-rpc/1/invoke');
$request->setMethod('POST');
$request->getPost()->fromString(json_encode($data));
$response = $client->send($request);
return $response;
}
回复内容如下:
["content":protected]=> string(87) "{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error","data":null},"id":null}
从 ZF2 Request Reference,你可以找到 setContent()
函数,你可以尝试用它代替 getPost()->fromString()
吗?
public function makeRequest()
{
$data = array(
'jsonrpc' => '2.0',
'method' => 'generateIntegers',
'params' => array(
'apiKey' => '00000000-0000-0000-0000-000000000000',
'n' => 10,
'min' => 1,
'max' => 10,
'replacement' => true,
"base" => 10),
'id' => 23866,
);
$client = new Client('https://api.random.org/json-rpc/1/invoke', array(
'adapter' => 'Zend\Http\Client\Adapter\Curl'
));
$client->setEncType(Client::ENC_FORMDATA);
$request = new Request();
$request->setUri('https://api.random.org/json-rpc/1/invoke');
$request->setMethod('POST');
//$request->getPost()->fromString(json_encode($data));
$request->setContent(json_encode($data));
$response = $client->send($request);
return $response;
}
我是 ZF2 的新手,我想创建一些利用外部 API 的应用程序。我已经成功创建了向 random.org API 发送请求的方法,但作为响应,它向我发送了一个与解析相关的错误。我不知道我做错了什么,也不知道如何改进我的方法以获得正确的响应数据。
这是我用来发送请求的方法(我有意更改了 api 密钥,在我的应用程序中我使用了正确的密钥):
public function makeRequest()
{
$data = array(
'jsonrpc' => '2.0',
'method' => 'generateIntegers',
'params' => array(
'apiKey' => '00000000-0000-0000-0000-000000000000',
'n' => 10,
'min' => 1,
'max' => 10,
'replacement' => true,
"base" => 10),
'id' => 23866,
);
$client = new Client('https://api.random.org/json-rpc/1/invoke', array(
'adapter' => 'Zend\Http\Client\Adapter\Curl'
));
$client->setEncType(Client::ENC_FORMDATA);
$request = new Request();
$request->setUri('https://api.random.org/json-rpc/1/invoke');
$request->setMethod('POST');
$request->getPost()->fromString(json_encode($data));
$response = $client->send($request);
return $response;
}
回复内容如下:
["content":protected]=> string(87) "{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error","data":null},"id":null}
从 ZF2 Request Reference,你可以找到 setContent()
函数,你可以尝试用它代替 getPost()->fromString()
吗?
public function makeRequest()
{
$data = array(
'jsonrpc' => '2.0',
'method' => 'generateIntegers',
'params' => array(
'apiKey' => '00000000-0000-0000-0000-000000000000',
'n' => 10,
'min' => 1,
'max' => 10,
'replacement' => true,
"base" => 10),
'id' => 23866,
);
$client = new Client('https://api.random.org/json-rpc/1/invoke', array(
'adapter' => 'Zend\Http\Client\Adapter\Curl'
));
$client->setEncType(Client::ENC_FORMDATA);
$request = new Request();
$request->setUri('https://api.random.org/json-rpc/1/invoke');
$request->setMethod('POST');
//$request->getPost()->fromString(json_encode($data));
$request->setContent(json_encode($data));
$response = $client->send($request);
return $response;
}