PHP -运行GET curl,获取结果


PHP - run GET curl, get result

我想在php a中运行GET curl从php b中获取数据

这是一个例子在php A(我从这里得到http://support.qualityunit.com/061754-How-to-make-REST-calls-in-PHP)

//next example will recieve all messages for specific conversation
$service_url = 'http://localhost/test/getFrom.php?id=1';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
    $info = curl_getinfo($curl);
    curl_close($curl);
    die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
    die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
var_export($decoded->response);

我也试过这个例子(尝试使用curl做一个GET,值被发送是允许null)

它将获取ID,运行一些脚本并生成一个ARRAY。

我想把这个数组从B变成a。

只有当A请求B的GET时,B才会运行。

问题是我不知道数组如何从B传递到a。

您提供的代码期望返回一个JSON编码的数组。最简单的方法是在PHP B中对数组进行JSON编码,并将其回显到页面。

CURL将能够读取PHP B的内容,并根据需要进行解码和处理。

// PHP B
<?php
  // Check for $_GET params  
  // Get ID 
  $id = $_GET['id'];
  // Do processing, query etc
  ....
  // Format and display array as JSON
  echo(json_encode($result_array));
  die();
?>

注意:

if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
    die('error occured: ' . $decoded->response->errormessage);
}

代码期望以特定的方式格式化数组。因此,要么将PHP B中的数组匹配为相同的格式,要么更新代码以满足您的需要。