开发者

Script to change all links on a page to curl requests

I curled a page. So I now have the page showing on my domain. The links on that page all point to the original page. I need a script that takes all the and replaces these links with the function

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'URL FROM THE A HREF OF THAT PARTICULAR LINK');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_response = curl_exec开发者_JAVA技巧($ch);
curl_close($ch);
echo $curl_response;

So when the links are clicked it does not direct you to the original page but runs the curl function and takes you to that page.

P.S thanks for the edit was about to do it but you got there first.


So you're trying to write a proxy? This has already been done, and open source.

You'll be wanting to make replacements on $curl_response. If the page uses relative paths, you could just append <base href="http://yourdomain.com" /> to the <head>. In addition, you could str_replace('http://theirdomain.com/', '') to remove any absolute paths.


Judging by your comments, the missing part you may be looking for could be PHP's DOM extension, or comparable. Basically, you want to parse the HTML of the page you're cURLing, so you can alter it.

You can do this by telling cURL to return the page source to you as a string (as you are doing), then feed it to DOMDocument::loadHTML. You would then grab all links using DOMDocument::getElementsByTagName (supplying 'a', possibly 'img' and a few others, as per your discretion).

Once you have that DOMNodeList, you can then alter their href-Attributes by retrieving the attribute with ...->attributes->getNamedItem('href') (that would be src for images, of course, if you want to go that far), tweaking the contents using the attribute's ...->nodeValue, e.g.:

$attributeNode->nodeValue = myURLtransform($attribute->nodeValue);

Finally, you would use DOMDocument::saveHTML to have the altered code, which you can then echo.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜