Php 使用参数器进行远程函数调用

Php 使用参数器进行远程函数调用,php,curl,Php,Curl,我使用这个函数在PHP中从一台服务器调用方法到另一台服务器 function get_url($request_url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $request_url); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec(

我使用这个函数在PHP中从一台服务器调用方法到另一台服务器

function get_url($request_url) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $request_url);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $response = curl_exec($ch);
  curl_close($ch);
}

$request_url = 'http://second-server-address/listening_page.php?function=somefunction';
$response = get_url($request_url);
这里我给出了一个带有函数名的URL。问题是如果函数收到的参数很少怎么办?如何使用CURL将参数传递给另一台服务器上的方法。

只需添加

$request_url = 'http://second-server-address/listening_page.php?function=somefunction&funcParam1=val&funcParam2.val

在函数中使用这些传递的参数

如果要将参数作为post请求传递,请尝试此操作

function post_to_url($url, $data) {
$fields = '';
foreach($data as $key => $value) { 
  $fields .= $key . '=' . $value . '&'; 
}
rtrim($fields, '&');

$post = curl_init();

curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POST, count($data));
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($post);

curl_close($post);
}

$data = array(
  "name" => "c.bavota",
  "website" => "http://bavotasan.com",
  "twitterID" => "bavotasan"
);

post_to_url("http://yoursite.com/post-to-page.php", $data);