我正在尝试使用php进行POST。在API中,它建议采用以下格式。
// post url, keys are added here.
{
"EmailAddress": "john.smith@acmeconsulting.co",
"ActivityEvent": 112,
"ActivityNote": "Note for the activity",
"ActivityDateTime": "yyyy-mm-dd hh:mm:ss",
"FirstName": "John",
"LastName" : "Smith",
"Phone" : "+919845098450",
"Score": 10
}
我的PHP代码:
$firstName='Test5';
$activityEvent=201;
$emailAddress='test10@test.com';
$activityNote='Note note note';
$phone='999999999';
$date='2015-07-21 12:48:10';
$data_string['ActivityEvent']=$activityEvent;
$data_string['EmailAddress']=$emailAddress;
$data_string['ActivityNote']=$activityNote;
$data_string['Phone']=$phone;
$data_string['ActivityDateTime']=$date;
//json_encode($data_string);
try
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json',
'Content-Length:'.strlen($data_string)
));
$json_response = curl_exec($curl);
curl_close($curl);
} catch (Exception $ex) {
curl_close($curl);
}
此代码未按预期工作。我没有得到任何更新。代码是否正确?
答案 0 :(得分:0)
你试过这个吗?
$object = new StdClass;
$object->ActivityEvent = 201;
$object->EmailAddress = 'john.smith@acmeconsulting.co';
//add more properties here
echo json_encode($object);
答案 1 :(得分:0)
$datastring = array(
'firstName' => 'John',
'lastName' => 'Smith',
...........);
$ch = curl_init("YOUR_URL_HERE");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $datastring );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
结果将存储在$result
变量