简体   繁体   English

php Curl单击链接

[英]php Curl clicked links

I need any link that has a "a href=" tag when clicked to be received via curl. 我需要任何点击时带有“ a href =“标记的链接,以通过curl接收。 I can't hard code these links as they are from a dynamic site so could be anything. 我无法对这些链接进行硬编码,因为它们来自动态站点,因此可以是任何东西。 How would I achieve this? 我将如何实现?

Thanks 谢谢

Edit: Let me explain more. 编辑:让我解释更多。 I have an app on my pc that uses a web front end. 我的PC上有一个使用Web前端的应用程序。 It catalogs files and gives yo options to rename delete etc. I want to add a public view however if I put it as is online then anyone can delete rename files. 它对文件进行分类并为您提供重命名删除等的选项。我想添加一个公共视图,但是如果我将其保持在线状态,则任何人都可以删除重命名文件。 If I curl the pages I can remove the menu bars and editing options through the use of a different css. 如果我卷曲页面,则可以通过使用其他CSS来删除菜单栏和编辑选项。 That part all works. 那部分全部起作用。 The only part that isn't working is if I click on a link on the page it directs me back to the original link address and that defeats the point as the menu bars are back. 唯一不起作用的部分是,如果我单击页面上的链接,它会将我定向回原始链接地址,并且由于菜单栏返回而使该点失效。 I need it to curl the clicked links. 我需要它来卷曲单击的链接。 Hope that makes more sense.. 希望这更有意义。

Here is my code that fetches the original link and curls that and changes the css to point to my own css. 这是我的代码,该代码获取原始链接并将其卷曲并更改css以指向我自己的css。 It points the java script to the original as I dont need to change that. 它将Java脚本指向原始脚本,因为我不需要更改它。 I now need to make the "a href" links on the page when clicked be called by curl and not go to the original destination 我现在需要在单击时使页面上的“ a href”链接被curl调用,而不是转到原始目标位置

<?php


$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'http://192.168.0.14:8081/home/');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_response = curl_exec($ch);
curl_close($ch);

//Change link url

$link = $curl_response;

$linkgo = '/sickbeard_public';
$linkfind = 'href="';
$linkreplace = 'href="' . $linkgo ;

$link = str_replace($linkfind, $linkreplace, $link);

//Change js url
$js = $link;

$jsgo = 'http://192.168.0.14:8081';
$jsfind = 'src="';
$jsreplace = 'src="' . $jsgo ;

$js = str_replace($jsfind, $jsreplace, $js);


//Fix on page link errors
$alink = $js;
$alinkgo = 'http://192.168.0.14:8081/';
$alinkfind = 'a href="/sickbeard_public/';
$alinkreplace = 'a href="' . $alinkgo ;

$alink = str_replace($alinkfind, $alinkreplace, $alink);

//Echo page back
echo $alink;

?>

You could grab all the URLs using a regular expression 您可以使用正则表达式获取所有URL

// insert general warning about how parsing HTML using regex is evil :-)
preg_match('/href="([^"]+)"/', $html, $matches);
$urls = array_slice($matches, 1);

// Now just loop through the array and fetch the URLs with cUrl...

While I can't imagine why you would do that I think you should use ajax. 虽然我无法想象为什么要这么做,但我认为您应该使用Ajax。 Attach an event on every a tag and send them to a script on your server where the magic of curl would happen. 在每个标签上附加一个事件,然后将其发送到服务器上的脚本,在该脚本上可能会发生卷曲。 Anyway you should explain why you need to fetch data with curl. 无论如何,您应该解释为什么需要使用curl来获取数据。

As far as I can understand your question you need to get the contents of URL via CURL... so here is the solution 据我了解您的问题,您需要通过CURL获取URL的内容...所以这是解决方案

<a href="<?php print $my_url; ?>" id="my_link">Click here to get via curl </a>

Then attach an event with the above <a> tag, eg in JQuery 然后使用上面的<a>标签附加一个事件,例如在JQuery中

$("#my_link").click(function(){
   var target_url = $(this).attr("href");
   //Send an ajax call to some of your page like cURL_wrapper.php with target_url as parameter in get
});

then in cURL_wrapper.php do follwoing 然后在cURL_wrapper.php操作

<?php
//Get the $target_url here from $_GET[];
$ch = curl_init($your_domain");
$fp = fopen("$target_url", "r");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM