Lisa8
Lisa8

Reputation: 179

PHP request data from HTTPS

First of all, I am new in programming. I want to request specific data from another server (data from MSSQL) and transfer (insert) into my MySQL DB, what's the proper way to gain this purpose?

PURPOSE:

A member from MSSQL side would like to transfer certain amount of points to MySQL, member could input how much of points they had, once the points is transfer into MySQL successful, a notify script would send to MSSQL side for update in their end.

What should be generate on MSSQL end, so that MySQL can get these data for further processing?

For example, I have HTTPS link: https://www.example.com/request-transfer.aspx?id=12345&sym=hello&name=linda

For PHP processing end, how can I use cURL to get these data?

I have below script for test, isn't enough to just use $_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;

Please advise.

Upvotes: 0

Views: 159

Answers (1)

Pinke Helga
Pinke Helga

Reputation: 6682

You code looks ok so far, just don't set the headers. They will be set automatically on GET/POST requests. Only on PUT requests you need to specify them.

You could JSON stringify the $_GET array directly. This would allow any parameters to be sent to the remote site. If you want to ensure only valid parameters may be sent, you have done it correct. You just should test if the GET parameter is actually present.

If you do not want repeated long expressions, you could write a short function:

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'));

Upvotes: 1

Related Questions