是否通過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']' –