使用cUrl从PHP发布JSON对象 [英] Post a JSON object from PHP using cUrl
本文介绍了使用cUrl从PHP发布JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经使用php功能json_encode
I have created a JSON object using php functionality json_encode
对象是通过以下方式制定的:
The Object is formulated this way:
$object_array[]=array('value1' => $value1, 'value2' => $value2);
$json_output = json_encode($object_array, JSON_UNESCAPED_SLASHES);
我需要使用PHP的'cUrl'功能将$json_output
发布到URL.
I need to post the $json_output
to a URL using the 'cUrl' functionality of PHP.
任何人都可以根据上述代码提出建议吗?
Can anyone suggest something based on the above code ?
推荐答案
使用curl发布json对象.
Post json objectusing curl.
$data = array('value1' => $value1, 'value2' => $value2);
$data_string = json_encode($data);
$ch = curl_init('http://api.local/rest/users'); // where to post
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
这篇关于使用cUrl从PHP发布JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文