在一个安静的后端服务上使用CURL帖子。在Firefox中使用RESTClient扩展,我得到了我想要的响应。
所以我在CURL中设置了授权、接受和内容类型标头
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_HTTPHEADER => array('User-Agent: Mozilla Firefox', 'Accept: application/json', 'Authorization: Basic v3iYdNlAcGxlKASocYkadkCSCIDpsdkFDJ='),
CURLOPT_URL => 'http://myurl.com/restfulservice/',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $jsonpoststring,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC | CURLAUTH_ANY,
CURLOPT_USERPWD => sprintf('%s;%s', 'foo@bar.com', 'somepassword'),
CURLOPT_SSL_VERIFYPEER => false
)
);
$output = curl_exec($ch);
curl_close($ch);
在RESTClient我有这个JSON在身体
{"authentication":{"identification":{"identstring":"foo@bar.com","identId":1},"security":{"securestring":"Password1", "secureId":3}},"externIdent":{"externAuth":{"hashauth":"K3DvhinSb7H","district":"iad"}}}
我的问题是:如何将那个人包括在PHPCurl请求中。
在后场传递
jsonpoststring='{"authentication":{"identification":{"identstring":"foo@bar.com","identId":1},"security":{"securestring":"Password1", "secureId":3}},"externIdent":{"externAuth":{"hashauth":"K3DvhinSb7H","district":"iad"}}}
';
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_HTTPHEADER => array('User-Agent: Mozilla Firefox', 'Accept: application/json', 'Authorization: Basic v3iYdNlAcGxlKASocYkadkCSCIDpsdkFDJ='),
CURLOPT_URL => 'http://myurl.com/restfulservice/',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $jsonpoststring,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC | CURLAUTH_ANY,
CURLOPT_USERPWD => sprintf('%s;%s', 'foo@bar.com', 'somepassword'),
CURLOPT_SSL_VERIFYPEER => false
)
);
$output = curl_exec($ch);
curl_close($ch);
这个应该可以了
$data = json_decode('{"authentication":{"identification":{"identstring":"foo@bar.com","identId":1},"security":{"securestring":"Password1", "secureId":3}},"externIdent":{"externAuth":{"hashauth":"K3DvhinSb7H","district":"iad"}}}');
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_HTTPHEADER => array('User-Agent: Mozilla Firefox', 'Accept: application/json', 'Authorization: Basic v3iYdNlAcGxlKASocYkadkCSCIDpsdkFDJ='),
CURLOPT_URL => 'http://myurl.com/restfulservice/',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC | CURLAUTH_ANY,
CURLOPT_USERPWD => sprintf('%s;%s', 'foo@bar.com', 'somepassword'),
CURLOPT_SSL_VERIFYPEER => false
)
);
$output = curl_exec