天天看點

curl模拟post請求Demo

function curl_post($url,$data){

    $data = http_build_query($data);//重點,改urlencode()編碼
    $ch = curl_init ();
    curl_setopt ( $ch, CURLOPT_URL, $uri );
    curl_setopt ( $ch, CURLOPT_POST,  );
    curl_setopt ( $ch, CURLOPT_HEADER,  );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER,  );
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
    $return = curl_exec ( $ch );
    curl_close ( $ch );
    return $return;
 }
 function qx_post(){

    $txtAmp = $this->input->post('txtAmp');
    $txtVolt = $this->input->post('txtVolt');
    $txtVm = $this->input->post('txtVm');
    $txtEata = $this->input->post('txtEata');
    $txtHmm = $this->input->post('txtHmm');
    $txtTamb = $this->input->post('txtTamb');
    $txtTph = $this->input->post('txtTph');
    $uri = 'http://www-it.jwes.or.jp/weld_simulator/en/calc2.jsp';
    // 參數數組
    $data = array (
            'txtAmp' => $txtAmp,
            'txtVolt' => $txtVolt,
            'txtVm' => $txtVm,
            'txtEata' => $txtEata,
            'txtHmm' => $txtHmm,
            'txtTamb' => $txtTamb,
            'txtTph' => $txtTph
    );
    $return = $this->curl_post($url,$data);
    $return = str_replace('Current','電流值',$return);
    $return = str_replace('<img src="/','<img src="http://www-it.jwes.or.jp/',$return);

    print_r($return);
}
           

//傳遞一個數組到CURLOPT_POSTFIELDS,cURL會把資料編碼成 multipart/form-data,而然傳遞一個URL-encoded字元串時,資料會被編碼成 application/x-www-form-urlencoded。