Jason Paddle
Jason Paddle

Reputation: 1103

Taking information from site which takes data from another site

I have following situation. 3 sites Site A, Site B and Site C.

Site B pulling with curl array from Site C like this:

Site B file 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);

Where ID is dynamical... Like this everything is fine. Now I'm trying from Site A following

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

So the question:

  1. The ID which Site B is pulling info about should come to Site B from Site A. How?

I'm also happy to see if there are other solutions here. Main part is Site A send ID to Site B and Site B pull from Site C return to Site B and Site B to Site A..

Upvotes: 2

Views: 70

Answers (3)

cetver
cetver

Reputation: 11829

Site B:

Set the additional response header

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

Site A:

Read the response header from 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=======

As I see that case:

You have Site B with URI site.b/getDataFromSiteC

The code that handles request path getDataFromSiteC looks like

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

Now if you make request to site.b/getDataFromSiteC in the response headers will additional header X-Site-C-ID: 10

You have Site A with URI site.a/getDataFromSiteB with following code:

$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;
}

Upvotes: 1

Raj Nandan Sharma
Raj Nandan Sharma

Reputation: 3862

Please try this code..

a.php //url a.php?id=123 ,accepts id as a parameter

<?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 will get the id value that propagated through b and is expected to return an array after some processing

Upvotes: 1

David J Eddy
David J Eddy

Reputation: 2037

From the formatting of the URLs my best guess is the data being returned is different. Cane we get examples of what each curl call is getting in return?

Upvotes: 0

Related Questions