首頁  >  文章  >  後端開發  >  php通过http post发送json数据

php通过http post发送json数据

WBOY
WBOY原創
2016-06-20 13:00:58995瀏覽

通过http post发送json数据

function http_post_data($url, $data_string) {

    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_POST, 1);
    
    curl_setopt($ch, CURLOPT_URL, $url);
    
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    
    'Content-Type: application/json; charset=utf-8',
    
    'Content-Length: ' . strlen($data_string))
    
    );

    ob_start();
    
    curl_exec($ch);
    
    $return_content = ob_get_contents();
    
    ob_end_clean();
    
    $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
    return array($return_code, $return_content);

}

$url  = "http://xx.xx.cn";

$data = json_encode(array('a'=>1, 'b'=>2));

list($return_code, $return_content) = http_post_data($url, $data);

 


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn