PHP: Send the Authorization token in a header instead of on the querystring and 读取 XML

  

<?php $opts = array(
    'http' => array(
     'method' => "GET",
     'header' => "Authorization: EFDDDDDFDDE&^*(EFFF/r/n"
    )
   );

      $context = stream_context_create($opts);
      $reqUrl = "http://www.123.com/tg/ad/find?advertiserId=123";
      if (is_numeric($index)) $reqUrl .= "&index=".$index;
      if (is_numeric($itemsPerPage)) $reqUrl .= "&itemsPerPage=".$itemsPerPage;

      $response = file_get_contents($reqUrl, false, $context);// 请求后返回的xml内容,格式如下:

?>

<?xml version="1.0" encoding="utf-8"?>
<MessageResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Status>Success</Status>
  <ExecutionTime>0:00:02.1718472</ExecutionTime>
  <Type>PagedCollection`1</Type>
  <Payload xsi:type="PagedCollectionOfAdvertiser">
    <Index>0</Index>
    <ItemsPerPage>20</ItemsPerPage>
    <TotalCount>13</TotalCount>
    <Items>
      <Advertiser>
        <Id>58e6227f-3761-49af-b49e-8bd5c59b7527</Id>
        <CompanyName>aaa-HeroTires-the-first-on-the-list</CompanyName>
      </Advertiser>
      <Advertiser>
        <Id>b9e5aefa-9d29-47b0-84d4-f1770a30f365</Id>
        <CompanyName>Cobra Kai Dojo</CompanyName>
      </Advertiser>
      <Advertiser>
        <Id>d266c582-2f85-4c20-a80f-f0e668c358ef</Id>
        <CompanyName>Hero DohNuts</CompanyName>
      </Advertiser>
      <Advertiser>
        <Id>768cf49e-5561-4d67-8b41-94d376c75894</Id>
        <CompanyName>Jack And Shinetech</CompanyName>
      </Advertiser>
      <Advertiser>
        <Id>a4137dc3-53c5-4489-91b1-a4f63d41a652</Id>
        <CompanyName>Jack And Shinetech</CompanyName>
      </Advertiser>
      <Advertiser>
        <Id>672f7a77-7361-474f-a871-62f9e3c9a64d</Id>
        <CompanyName>Jack.shinetech.1103290102</CompanyName>
      </Advertiser>
      <Advertiser>
        <Id>bee8e657-e8e4-4f95-8c04-a630e9dce4ca</Id>
        <CompanyName>Jack.shinetech.1103290243</CompanyName>
      </Advertiser>
      <Advertiser>
        <Id>a76004ae-a8c2-4b31-869c-43574340738e</Id>
        <CompanyName>Jack.shinetech.1103290440</CompanyName>
      </Advertiser>
      <Advertiser>
        <Id>588aaad8-4227-4d43-9d1f-8567795c2e75</Id>
        <CompanyName>Jack.shinetech.1103290730</CompanyName>
      </Advertiser>
      <Advertiser>
        <Id>ea7c751a-78dd-449b-a70c-3080f108c061</Id>
        <CompanyName>Jack.shinetech.1103291916</CompanyName>
      </Advertiser>
    </Items>
  </Payload>
</MessageResponse>
读取节点值并显示:
<?php
    require_once('configvar.php');
    $reqUrl = $API_PATH . "/tg/advertiser/list?";
   $opts = array(
    'http' => array(
     'method' => "GET",
     'header' => "Authorization: " . $AUTH_CODE . "/r/n"
    )
   );
      $context = stream_context_create($opts);
      $response = file_get_contents($reqUrl, false, $context);
  
      $doc = DOMDocument::loadXML($response, LIBXML_NOBLANKS);
   $doc->formatOutput = true;
      $advertisers = $doc->getElementsByTagName("Advertiser");
      ?>
   <table border='1'>
      <tr height='20px'><td width='300px'><b>Advertiser Id</b></td><td width='300px'><b>Company Name</b></td></tr>
   <?php
      foreach($advertisers as $advertiser)
     {
    $Ids = $advertiser->getElementsByTagName("Id");
    $Id = $Ids->item(0)->nodeValue;  
   
    $CompanyNames = $advertiser->getElementsByTagName("CompanyName");
    $CompanyName = $CompanyNames->item(0)->nodeValue;
  ?>
   
  <tr height="20px"><td><?php echo $Id ?></td><td><a href="CheckAdFind_get.php?advertiserId=<?php echo $Id ?>"><?php echo $CompanyName ?></a></td></tr>
  <?php
      }
   ?>
      </table>
   <h2>Response XML</h2>
   <pre><code class="xml"><?php echo htmlspecialchars($doc->saveXML()) ?></code></pre>

你可能感兴趣的:(PHP: Send the Authorization token in a header instead of on the querystring and 读取 XML)