我使用以下代码在数组中传递值&我已经在后台设置了fatherName。如何传递"parentName"的值&检索其值。我正在尝试检索,但它们是一个空的响应错误。任何帮助都将不胜感激。。!!
<?php
$uname = "";
$pass = "";
$client_id = "";
$organizerKey = "";
$webinar_key = '';
$registrantKey = '';
$url = "https://api.citrixonline.com/G2W/rest/organizers/$organizerKey/webinars/$webinar_key/registrants";
$access_token = "";
function runThis($url) {
$access_token = "";
$data = array("firstName"=>"test22","lastName"=>"test11","email"=>"abc@gmail.com","city"=>"abc","state"=>"abc","country"=>"xyz","phone"=>"1230","jobTitle"=>"ZX","responses"=>array(array("questionKey"=>3392655),array("answerswerKey"=>3392656)));
$data_string = json_encode($data);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$headers = array(
'Accept: application/json',
'Accept: application/vnd.citrix.g2wapi-v1.1+json',
'Content-type: application/json',
"Authorization: OAuth oauth_token=$access_token"
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
return curl_exec($curl);
}
$result = runThis($url);
print_r($result);
// wrap numbers
$result = json_decode(preg_replace('/("'w+"):('d+('.'d+)?)/', '''1:"''2"', $result),true);
echo "<pre>";
echo "<hr>";
print_r($result);
echo "</pre>";
?>
如果您使用JSON编码发送,则需要使用在php中检索RAW POST数据
file_get_contents(“php://input”);
然后您可以正常解码JSON数据:
$json = json_decode(file_get_contents(“php://input”));
也可以使用遗留方法(但这将在PHP 5.6中折旧):
$json = json_decode($HTTP_RAW_POST_DATA);
这是因为,当读取$_POST
变量时,PHP希望接收到它的本地编码(key_1=val_1&key_2=val_2)
等,因此总是返回空!
举个例子:
send.php
//set POST variables
$url = 'http://mypage.com';
$fields = array(
'lname' => 'Last,
'fname' => 'First'
);
$count = count($fields)
$fields = json_encode($fields);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, $count);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
您刚刚向'发送了POSThttp://mypage.com'编码为JSON。
receive.php
要接收您刚刚发送的JSON编码数据,您必须读取原始后期数据
$json = json_decode(file_get_contents(“php://input”));
var_dump($json); //do something with $json data
http://php.net/manual/en/reserved.variables.httprawpostdata.php