PHP 以 Curl 傳遞 POST 資料,並取得回傳值

Curl 傳遞 POST 資料,並取得回傳值

/**
* VECTOR COOL
* https://vector.cool
*/
//用curl傳post並取回傳值
//一定要傳絕對路徑
function curl_post($url,$post)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close ($ch);
return $result;
}

使用範例:

/**
* VECTOR COOL
* https://vector.cool
*/
$url='http://tw.yahoo.com';
$post_value= array(
'name' => 'JACK',
'age' => '20',
'phone' => '0968123456',
'address' => '台灣'
);

echo curl_post($url,$post_value);

 [……]

閱讀更多

jQuery get all form values 取表單所有的值

// 方法1
var datastring = $("#form").serialize();
$.ajax({
type: "POST",
url: "test.php",
data: datastring,
success: function(data) {
alert('Data send');
}
});

//方法2
var form = $(this).closest('form');
var data = form.serializeArray();
data.push({name: "fun", value: 'img_edit'}); //增加自訂的值
data.push({name: "time", value: $.now()}); //增加自訂的值
data = $.param(data);
$.ajax({
type: "POST",
url: "test.php",
data: data,
success: function(data) {
alert('Data send');
}
});

test.php 測試
<?php
  print_r($_POST);
exit;
?>

結果
Array (
  [mail] => ann71727@test.com.tw 
  [password] => asdfasdf
  [name] => test
  [sex] => 1
)

 
https://api.jquery.com/serialize/



[……]

閱讀更多