从站点获取信息从另一个站点获取数据

时间:2017-02-15 14:07:19

标签: php curl

我有以下情况。 3个网站Site ASite BSite C

Site B使用Site C中的curl数组拉这样:

Site B档案data.php

function get_curl_content_tx($url) {
      $curl = curl_init();
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl, CURLOPT_HEADER, false);
      $result = curl_exec($curl);
      curl_close($curl);
      return $result;
}
$url=get_curl_content_tx("http://Site_C_domain/ask/ID");

$total =json_decode($url,true);

ID是动态的......就像这样一切都很好。现在我正在尝试Site A跟随

function get_curl_content_from_site_b($url) {
      $curl = curl_init();
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl, CURLOPT_HEADER, false);
      $result = curl_exec($curl);
      curl_close($curl);
      return $result;
}
$url=get_curl_content_tx("http://Site_B_domain/data.php");

$total =json_decode($url,true);

var_dump($total);

所以问题是:

  1. ID提取信息的Site B应该来到网站B. 来自Site A. How?
  2. 我也很高兴看到这里有其他解决方案。主要部分是站点A向站点B发送ID,站点B从站点C返回站点B,站点B从站点A返回..

3 个答案:

答案 0 :(得分:1)

网站B:

设置附加响应标题

header('X-Site-C-ID: 10');

网站A:

阅读Site B的响应标题

$siteCId = null;
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $line) use (&$siteCId) {
    $headerName = 'X-Site-C-ID: ';
    if (strpos($line, $headerName) !== false) {
        $siteCId = trim($line, "$headerName\n");
    }
    return strlen($line);
});
$response = curl_exec($ch);
var_dump($siteCId);

<强> ======= UPDATE =======

正如我所看到的那样:

您有Site B URI site.b/getDataFromSiteC

处理请求路径getDataFromSiteC的代码看起来像

function get_curl_content_tx($url) {
      $curl = curl_init();
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl, CURLOPT_HEADER, false);
      $result = curl_exec($curl);
      curl_close($curl);
      return $result;
}

$id = 10;
$url = get_curl_content_tx("http://site.c/ask/$id");    
$total = json_decode($url, true);
// some actions with $total ...

// set the additional header
header("X-Site-C-ID: $id");

现在,如果您在响应标头中向site.b/getDataFromSiteC发出请求,则会有额外的标头X-Site-C-ID: 10

您拥有Site A URI site.a/getDataFromSiteB,代码如下:

$siteCId = null;
$ch = curl_init('site.b/getDataFromSiteB');
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $line) use (&$siteCId) {
    $headerName = 'X-Site-C-ID: ';
    if (strpos($line, $headerName) !== false) {
        $siteCId = trim($line, "$headerName\n");
    }
    return strlen($line);
});
$result = curl_exec($curl);
curl_close($curl);
if ($siteCId !== null) {
   // you get Site C ID through Site B
   echo $siteCId;
}

答案 1 :(得分:1)

请尝试此代码..

  

a.php // url a.php?id = 123,接受id作为参数

<?php
function get_curl_content_from_site_b($url) {

    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => $url,
        CURLOPT_HEADER=> 0,
        CURLOPT_RETURNTRANSFER=> 0,
        CURLOPT_SSL_VERIFYPEER=> 0
    ));
    $resp = curl_exec($curl);   

      curl_close($curl);
      return $resp;
}
$url=get_curl_content_from_site_b("http://localhost/so/b.php?id=".$_GET["id"]);

?>
  

b.php

<?php
function get_curl_content_tx($url) {
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => $url,
        CURLOPT_HEADER=> 0,
        CURLOPT_RETURNTRANSFER=> 0,
        CURLOPT_SSL_VERIFYPEER=> 0
    ));
    $resp = curl_exec($curl);   

      curl_close($curl);
      return $resp;


}
$url=get_curl_content_tx("http://localhost/so/c.php?id=".$_GET["id"]);
?>
  

c.php

<?php
    print_r(array($_GET["id"],23));exit;        
?>

c.php将获取通过b传播的id值,并且在某些处理之后会返回一个数组

答案 2 :(得分:0)

根据URL的格式,我最好的猜测是返回的数据不同。甘蔗我们得到每个卷曲调用得到的回报的例子吗?