我正在研究Commission Junction api,我想从CJ中提取链接url和图像网址,所以我已经创建了这个脚本但输出不是我预期的例如。你可以看到echo $ mat [0]丢失了,所以我该如何解决这个问题呢? 感谢
Code:
$URI = 'https://linksearch.api.cj.com/v2/link-search?'.
'website-id=1234567'.
'&advertiser-ids=adid'.
'&link-type=text+link'.
'&records-per-page=1';
$context = stream_context_create(
array('http' =>
array(
'method' => 'GET',
'header' => 'Authorization: 'myapiid'
)
)
);
$data = new SimpleXMLElement(file_get_contents($URI, false, $context));
foreach ($data->links[0] as $link)
{
// Sanitize data.
$pd = $link->{'link-code-html'};
echo var_dump($pd);
preg_match('#<img\s+src\s*=\s*"([^"]+)"#i',$pd,$mat);
preg_match('#<a\s+href\s*=\s*"([^"]+)"[^>]*>([^<]+)</a>#i',$pd,$matches);
echo '<pre>';
print_r($matches);
echo $matches[1];
print_r($mat);
echo $mat[0];
echo '</pre>';
}
OUTPUT:
object(SimpleXMLElement)[5]
string '<a href="http://www.dpbolvw.net/click-1234567-42452100">Save up to 70% on Airport Parking</a><img src="http://www.tqlkg.com/image-1234567-42452100" width="1" height="1" border="0"/>' (length=181)
Array
(
[0] => Save up to 70% on Airport Parking
[1] => http://www.dpbolvw.net/click-1234567-42452100
[2] => Save up to 70% on Airport Parking
)
http://www.dpbolvw.net/click-1234567-42452100Array
(
[0] => http://www.tqlkg.com/image-1234567-42452100
)
答案 0 :(得分:1)
您在授权
中缺少单引号$context = stream_context_create(
array('http' => array(
'method' => 'GET',
'header' => 'Authorization' : 'myapiid'
)
)
);
答案 1 :(得分:0)
您要查找的网址位于
$mat[1]
不在
$mat[0]
尝试使用var_dump而不是print_f。 Print_f读取更容易,但var_dump有时会提供更多信息。