是否通过CURL
发送$json
数据的正确方法?在PHP,通过POST发送JSON数据的正确方法php
我有我的$json
数据在一个php中,我可以通过POST
使用CURL
我的api发送数据信息,??
你有一些例子,非常感谢!
是否通过CURL
发送$json
数据的正确方法?在PHP,通过POST发送JSON数据的正确方法php
我有我的$json
数据在一个php中,我可以通过POST
使用CURL
我的api发送数据信息,??
你有一些例子,非常感谢!
请找前一篇:
http://bavotasan.com/2011/post-url-using-curl-php/
在上$数据的一些关键,把你的$ JSON。
例
function post_to_url($url, $data) {
$fields = '';
foreach($data as $key => $value) {
$fields .= $key . '=' . $value . '&';
}
rtrim($fields, '&');
$post = curl_init();
curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POST, count($data));
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($post);
curl_close($post);
}
$json = json_encode($your_json); //Your json data
$data = array(
"name" => "c.bavota",
"website" => "http://bavotasan.com",
"twitterID" => "bavotasan",
"json" => $json
);
post_to_url("http://yoursite.com/post-to-page.php", $data);
是。你可以做。
例如,
// $json_string : Your json String
$ch = curl_init('http://api.example.com/post');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($json_string))
);
$result = curl_exec($ch);
但问题是$ jsondata = json_encode($ json);没有获取我的数据卷曲,就像我的JSON没有什么,当我刷新页面我得到了服务器的响应,并说,没有数据,但如果我做回声json_encode($ json);我得到了字符串 – Deimos
@Deimos对不起。你有'json'字符串没有准备好? –
@Jeson M John,你是说如果我得到json的输出? http://stackoverflow.com/questions/21333828/send-json-data-php-via-curl/21333968?noredirect=1#comment32224252_21333968在这里我把更多的信息,请帮助我:) – Deimos
MMMM都在同一个页面吗?只有当我刷新页面应该发送的数据? – Deimos
但问题是$ jsondata = json_encode($ json);没有获取我的数据卷曲,就像我的JSON没有什么,当我刷新页面我得到了服务器的响应,并说,没有数据,但如果我做回声json_encode($ json);我得到了字符串 – Deimos
我更新了我的答案。尝试一下。发布后尝试获取json。 'echo $ _POST ['json']' –