2014-01-27 98 views

回答

0

請找前一篇:
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); 
+0

MMMM都在同一個頁面嗎?只有當我刷新頁面應該發送的數據? – Deimos

+0

但問題是$ jsondata = json_encode($ json);沒有獲取我的數據捲曲,就像我的JSON沒有什麼,當我刷新頁面我得到了服務器的響應,並說,沒有數據,但如果我做回聲json_encode($ json);我得到了字符串 – Deimos

+0

我更新了我的答案。嘗試一下。發佈後嘗試獲取json。 'echo $ _POST ['json']' –

1

是。你可以做。

例如,

// $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); 
+0

但問題是$ jsondata = json_encode($ json);沒有獲取我的數據捲曲,就像我的JSON沒有什麼,當我刷新頁面我得到了服務器的響應,並說,沒有數據,但如果我做回聲json_encode($ json);我得到了字符串 – Deimos

+0

@Deimos對不起。你有'json'字符串沒有準備好? –

+0

@Jeson M John,你是說如果我得到json的輸出? http://stackoverflow.com/questions/21333828/send-json-data-php-via-curl/21333968?noredirect=1#comment32224252_21333968在這裏我把更多的信息,請幫助我:) – Deimos

相關問題