如何在浏览器中检索当前url并嵌入到php文件中

如何在浏览器中检索当前url并嵌入到php文件中,php,url,pdf,browser,Php,Url,Pdf,Browser,您好,我正在使用一个免费的api将网页转换为pdf,通过在代码段中输入url,它可以完美地工作,但我想要实现的是将动态生成的页面转换为pdf,但是我没有正确的方法来实现这一点,我尝试将动态url发布到php文件中,但它不起作用,因此,我问是否有一种方法可以获取浏览器的当前url,并将其附加到要放置url的位置 这就是我尝试过的 <?php $pdf = file_get_contents('http://api.simplehtmltopdf.com/?link=$_POS

您好,我正在使用一个免费的api将网页转换为pdf,通过在代码段中输入url,它可以完美地工作,但我想要实现的是将动态生成的页面转换为pdf,但是我没有正确的方法来实现这一点,我尝试将动态url发布到php文件中,但它不起作用,因此,我问是否有一种方法可以获取浏览器的当前url,并将其附加到要放置url的位置

这就是我尝试过的

<?php
        $pdf = file_get_contents('http://api.simplehtmltopdf.com/?link=$_POST[link]&orientation=Portrait&mtop=10&mright=10&mleft=10&mbot=0');
        header("Content-Type: application/pdf");
        header("Cache-Control: no-cache");
        header("Accept-Ranges: none");
        header("Content-Disposition: attachment; filename=\"your_pdf_name.pdf\"");
        echo $pdf;
?>
这是最开始的代码片段

 <?php
        $pdf = file_get_contents('http://api.simplehtmltopdf.com/?link=http://google.com&orientation=Portrait&mtop=10&mright=10&mleft=10&mbot=0');
        header("Content-Type: application/pdf");
        header("Cache-Control: no-cache");
        header("Accept-Ranges: none");
        header("Content-Disposition: attachment; filename=\"your_pdf_name.pdf\"");
        echo $pdf;
?>

通过使用php获取页面的当前url

function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
 $current_page_url = curPageURL();
然后使用curl

比如说

$Curl_Session = curl_init('url of your php file which one convert web page to pdf');
                    curl_setopt ($Curl_Session, CURLOPT_POST, 1);
                    curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "current_url=**$current_page_url** ");
                    curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
                    $res = curl_exec ($Curl_Session);
                    curl_close ($Curl_Session);
然后在pdf_转换器文件中进行以下更改

<?php
        $current_url = $_POST['current_url'];
        $pdf = file_get_contents('http://api.simplehtmltopdf.com/?link=**$current_url**');
        header("Content-Type: application/pdf");
        header("Cache-Control: no-cache");
        header("Accept-Ranges: none");
        header("Content-Disposition: attachment; filename=\"your_pdf_name.pdf\"");
        echo $pdf;
?>
试试下面的代码

$page = "http://" . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];


<?php
$pdf = file_get_contents('http://api.simplehtmltopdf.com/?link='.$page.'&orientation=Portrait&mtop=10&mright=10&mleft=10&mbot=0'); 
header("Content-Type: application/pdf"); 
header("Cache-Control: no-cache");
header("Accept-Ranges: none"); 
header("Content-Disposition: attachment; filename=\"your_pdf_name.pdf\"");
echo $pdf;
或用于带有查询字符串的URL

<?php
    $pdf = file_get_contents('http://api.simplehtmltopdf.com/?link='.$page.$_SERVER['QUERY_STRING'].'&orientation=Portrait&mtop=10&mright=10&mleft=10&mbot=0'); 
    header("Content-Type: application/pdf"); 
    header("Cache-Control: no-cache");
    header("Accept-Ranges: none"); 
    header("Content-Disposition: attachment; filename=\"your_pdf_name.pdf\"");
    echo $pdf;

那么我该如何应用它呢?我给PLS的代码片段,它的文件用动态url显示动态内容??cssss.php?Transaction_id=ksksdgsgshdgsdfshvc026265275373232327224sbxsdyusxsemceiuhqcdxsikmreadbfswuk让我们试试这个。。在这个cssss.php文件中,复制第一个代码段,并尝试回显$current_page_url值。如果有效,则复制第二个代码段。这里的主要变化是设置您的pdf_转换器文件Pathaite让我试试这个