我有以下网址:
http://sowacs.appspot.com/AWS/webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=AKIAICNDB2XMNUH7OIDA&AssociateTag=sometag&BrowseNode=10777&Operation=ItemSearch&SearchIndex=Books&Condition=All&ResponseGroup=ItemIds&ItemPage=1
当您单击指向xml文件的链接时。当您查看Url时,会添加时间戳和签名参数。我想在PHP脚本中获得这个扩展的Url。我正在寻找几个小时,发现像:
function get_web_page( $url )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => true, // return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
//$header['errno'] = $err;
// $header['errmsg'] = $errmsg;
//$header['content'] = $content;
print($header[0]);
return $header;
}
$thisurl = "http://sowacs.appspot.com/AWS/
webservices.amazon.com/onca/xml?
Service=AWSECommerceService&
AWSAccessKeyId=AKIAICNDB2XMNUH7OIDA&AssociateTag=sometag&BrowseNode=10777
&Operation=ItemSearch&SearchIndex=Books&Condition=All&ResponseGroup=ItemIds&ItemPage=1
";
$myUrlInfo = get_web_page( $thisurl );
echo $myUrlInfo["url"];
但这只是第一个网址。不是扩展的。我需要这个重定向来满足amazon-api请求。此站点为我生成签名和时间戳(https://sowacs.appspot.com/),以便我执行api请求。也许这不是一个“正常”的重定向。我不知道,我是相对较新的编码...所以我的PHP脚本需要网址! THX。
答案 0 :(得分:1)
在$header
数组中,您获得了return_url
密钥,其中包含Amazon AWS API URL,并在请求正文中添加了时间戳和签名。
实施的一个例子:
function get_web_page( $url )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => true, // return headers
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
return $header['redirect_url'];
}
$thisurl = "http://sowacs.appspot.com/AWS/webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=AKIAICNDB2XMNUH7OIDA&AssociateTag=sometag&BrowseNode=10777&Operation=ItemSearch&SearchIndex=Books&Condition=All&ResponseGroup=ItemIds&ItemPage=1";
// Get Amazon AWS URL
$url = get_web_page($thisurl);
echo $url;
输出结果与帖子顶部的示例结构相同,包括时间戳和签名:
http://webservices.amazon.com/onca/xml?AWSAccessKeyId=AKIAICNDB2XMNUH7OIDA&AssociateTag=sometag&BrowseNode=10777&Condition=All&ItemPage=1&Operation=ItemSearch&ResponseGroup=ItemIds&SearchIndex=Books&Service=AWSECommerceService&Timestamp=2014-09-01T03%3A18%3A02.000Z&Signature=IxRHgn7AV2YFq05I6Q33eF3aJuzEqsM5e7e1lW1xG8E%3D
在这里,您可以使用该URL来处理该URL,但是例如,您可以使用PHP获取XML文档的内容:
$xml = file_get_contents($url);
...然后使用DOM解析器来处理它,例如:
$doc = new DOMDocument();
$doc->loadXML($xml);
$items = $doc->getElementsByTagName('Item');
foreach($items as $item) {
echo '<li>ASIN: ' . $item->nodeValue . '</li>', PHP_EOL;
}
输出:
<li>ASIN: 1454824573</li>
<li>ASIN: 0615361161</li>
<li>ASIN: 1454807105</li>
<li>ASIN: 1285185242</li>
<li>ASIN: 0314279822</li>
<li>ASIN: 1599417049</li>
<li>ASIN: 0735598223</li>
<li>ASIN: 1454851368</li>
<li>ASIN: 1133191355</li>
<li>ASIN: 1454815485</li>
...或将用户重定向到它:
header("Location: " . $url);
exit;
既然你提到你不熟悉编码,我会解释一下how Amazon's API works: