我正在尝试连接到外部站点并将产品带到那里,并使用其使用者和secret keys
将它们复制到另一个站点。
我有一个URL链接,该链接从外部站点1进行链接。
现在,我使用JSON
CURL获取了所有PHP
数据(包括销售,图像,数据等)
代码如下:
function get_wc_api_client() {
$curl = curl_init();
$site = 'https://samplelink.com/wp-json/wc/v2/products?consumer_key=sample-consumer_key&consumer_secret=sample_consumer_secret';
curl_setopt($curl, CURLOPT_URL, $site );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
$content = curl_exec($curl);
echo "<pre>";
print_r( json_decode($content, true)) ;
echo "</pre>";
}
任何建议如何获取外部站点产品的图像和标题,以及每个产品的链接并将其显示为完整图像和文本,而不是JSON文本?
预先感谢
更新: 这是print_r
的结果示例代码Array
(
[0] => Array
(
[id] => 4591
[name] => Magic Forest
[slug] => magic-forest
[permalink] => https://samplesite.com/product/magic-forest/
[date_created] => 2019-03-13T19:11:09
[date_created_gmt] => 2019-03-13T19:11:09
[date_modified] => 2019-03-13T19:12:20
[date_modified_gmt] => 2019-03-13T19:12:20
[type] => simple
[status] => publish
[featured] =>
[catalog_visibility] => visible
[description] =>
但是正如我所说,我只需要产品的图像,标题和它们的链接,并将它们视为真实的图像,标题和链接。
[images] => Array
(
[0] => Array
(
[id] => 4932
[date_created] => 2019-03-13T18:54:54
[date_created_gmt] => 2019-03-13T18:54:54
[date_modified] => 2019-03-13T18:55:14
[date_modified_gmt] => 2019-03-13T18:55:14
[src] => https://samplesite.com/wpcom-129014632/wp-content/uploads/2019/03/Pond-image.jpg
[name] => Pond Cover
[alt] =>
[position] => 0
)
对于图像:执行时,我需要image src链接为Real图像,同时还要为产品标题。
有可能吗?
答案 0 :(得分:0)
使用以下代码
$apiData = get_wc_api_client();
foreach($apiData['images'] as $data){
echo '<image src="'.$data['src'].'" title="'.$data['name'].'" alt="'.$data['alt'].'">';
}
在视图页面中调用此功能
{{1}}