从网址获取元标记的另一种方法

时间:2018-10-31 19:35:50

标签: php wordpress

我正在使用脚本从url获取元标记:

function post_updated_set_og_image( $post_id ) {

    $url = get_field('link', $post_id);
    $page_content = file_get_contents($url);

    $dom_obj = new DOMDocument();
    @$dom_obj->loadHTML($page_content);
    $meta_val = null;

    foreach($dom_obj->getElementsByTagName('meta') as $meta) {

      if($meta->getAttribute('property')=='og:image'){ 

      $meta_val = $meta->getAttribute('content');
    }

    update_post_meta($post_id, 'og_image_src', $meta_val);

}}
add_action( 'save_post', 'post_updated_set_og_image' );

仅在localhost上有效,服务器给我错误:

  

警告:file_get_contents($ url):无法打开流:HTTP请求   失败了!禁止使用HTTP / 1.1 403 ...

是从url获取元标记的另一种方法,该怎么做或如何修复上面的代码?我已经检查了php选项,并且allow_url_fopen已打开。

0 个答案:

没有答案
相关问题