PHP从HTTPS请求数据


PHP request data from HTTPS

首先,我是编程新手。我想从另一台服务器请求特定的数据(来自MSSQL的数据),并将其传输(插入)到我的MySQL数据库中,获得此目的的正确方法是什么?

目的:

MSSQL方面的成员希望转移一定数量的点到MySQL,成员可以输入他们有多少点,一次要点是成功转移到MySQL,通知脚本会发送到MSSQL端进行更新。

MSSQL端应该生成什么,以便MySQL可以获得这些数据进行进一步处理?

例如,我有HTTPS链接:https://www.example.com/request-transfer.aspx?id=12345&sym=hello&name=linda

对于PHP处理端,我如何使用cURL来获取这些数据?

我有下面的测试脚本,仅仅使用$_GET还不够吗?

// GET
$id   = $_GET['id'];
$sym  = $_GET['sym'];
$name = $_GET['name'];
$data = array("id" => $id, "sym" => $sym, "name" => $name);
$data_string = json_encode($data);
$ch = curl_init('http://localhost/test/process.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$input = json_decode($data_string);
echo $input->id.', '.$input->sym.', '.$input->name; 
echo $result;

请告知。

到目前为止,您的代码看起来还不错,只是没有设置头。它们将在GET/POST请求时自动设置。只有在PUT请求上,才需要指定它们。

您可以直接对$_GET数组进行JSON字符串化。这将允许将任何参数发送到远程站点。如果您想确保只能发送有效的参数,那么您已经做得正确了。您只需要测试GET参数是否真的存在。

如果你不想重复长表达式,你可以写一个短函数:

function _GET()
{ $n = func_num_args();
  for($i = 0 ; $i<$n ;)
  { $key = func_get_arg($i++);
    $array[$key] = isset($_GET[$key]) ? $_GET[$key] : '';
  }
  return $array;
}
$data_string = json_encode(_GET('id', 'sym', 'name'));