PHP:相对于curl'ed html中的绝对URL [英] PHP: Relative to absolute URLs in curl'ed html

查看:103
本文介绍了PHP:相对于curl'ed html中的绝对URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP中的curl检索简单的HTML页面:

I am retrieving a simple HTML page using curl in PHP:

<?php
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, "http://example.com/server");
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_exec($ch);
 curl_close($ch);
?>

并将包括放入文件我的网站:

and includeing it in a file on my website:

<?php include('curlingCodeAbove.php');?>

我想替换 curl 带有绝对URL的HTML。我对将相对URL更改为绝对URL 很熟悉,但是我尝试将该解决方案插入此代码中未成功。

I want to replace the relative URLs in the curled HTML with absolute URLs. I am familiar with Change a relative URL to absolute URL but I have tried unsuccessfully to insert that solution into this code.

推荐答案

自己修复。设置 CURLOPT_RETURNTRANSFER 标志。

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/server");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
$result = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+)#",'$1http://mydomain.com/$2$3', $result);
echo $result
?>

虽然接受您自己的答案可能是不好的形式,嘿,我就是那个想出来的人。

While it's probably bad form to accept your own answer, hey, I'm the one who figured it out.

这篇关于PHP:相对于curl'ed html中的绝对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆