Showing posts with label header. Show all posts
Showing posts with label header. Show all posts

Friday, June 22, 2018

Header only retrieval in php via curl | PHP CURL get content type from URL | PHP CURL retrieve headers information from URL

Response headers can contain valuable information and may help to keep your API responses simpler by separating the actual response data from accessory metadata.
For instance, when querying the API for a list of posts, the response body includes just the content but there are also some other valualbe information sent as headers:
The PHP code for the CURL request would look something like this:
$agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36 X-Client-Data: CIa2yQEIpLbJAQjBtskBCKmdygEIqKPKARiSo8oB";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FILETIME, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_USERAGENT, $agent);
$header = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
Which will output as below:
HTTP/1.1 200 OK
Date: Fri, 22 Jun 2018 06:47:27 GMT
Server: Apache/2.4.33 (Win32) OpenSSL/1.0.2n PHP/5.6.35
Last-Modified: Sun, 11 Feb 2018 16:39:41 GMT
ETag: "5ae6f-564f2687e28f8"
Accept-Ranges: bytes
Content-Length: 372335
Content-Type: application/pdf

Array
(
    [url] => http://localhost/text-finder/download.pdf
    [content_type] => application/pdf
    [http_code] => 200
    [header_size] => 265
    [request_size] => 258
    [filetime] => 1518367181
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.016
    [namelookup_time] => 0.016
    [connect_time] => 0.016
    [pretransfer_time] => 0.016
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => 372335
    [upload_content_length] => -1
    [starttransfer_time] => 0.016
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => ::1
    [certinfo] => Array
        (
        )

    [primary_port] => 80
    [local_ip] => ::1
    [local_port] => 51877
)


HTTP/1.1 200 OK
Date: Fri, 22 Jun 2018 06:48:11 GMT
Server: Apache/2.4.33 (Win32) OpenSSL/1.0.2n PHP/5.6.35
Last-Modified: Fri, 22 Jun 2018 04:18:28 GMT
ETag: "92-56f3352eebe85"
Accept-Ranges: bytes
Content-Length: 146
Content-Type: text/html

Array
(
    [url] => http://localhost/text-finder/test2.html
    [content_type] => text/html
    [http_code] => 200
    [header_size] => 253
    [request_size] => 256
    [filetime] => 1529641108
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.015
    [namelookup_time] => 1.0E-6
    [connect_time] => 0.015
    [pretransfer_time] => 0.015
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => 146
    [upload_content_length] => -1
    [starttransfer_time] => 0.015
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => ::1
    [certinfo] => Array
        (
        )

    [primary_port] => 80
    [local_ip] => ::1
    [local_port] => 51944
)

Monday, May 15, 2017

How to add header/footer to multipage pdf on Laravel SnappyPDF WkHtmlToPdf

I am sure you already install WkhtmlToPDF with Snappy in your Laravel project. If not yet please visit the link below to install WkhtmltoPDF in your Laravel project:

Click here.

Now add margin for your page if not added yet because header and footer need some space to visible in PDF header and footer area by editing the file named "config/snappy.php".

<?php
return array(
    'pdf' => array(
        'enabled' => true,
        'binary'  => 'C:\PROGRA~1\wkhtmltopdf\bin\wkhtmltopdf.exe',
        'timeout' => false,
        'options' => array(
            'margin-top'    => 15,
            'margin-right'  => 8,
            'margin-bottom' => 20,
            'margin-left'   => 5
        ),
        'env'     => array(),
    ),
    'image' => array(
        'enabled' => true,
        'binary'  => 'C:\PROGRA~1\wkhtmltopdf\bin\wkhtmltoimage.exe',
        'timeout' => false,
        'options' => array(),
        'env'     => array(),
    )

);


And then append header and footer html in your PDF as follows:

$header_html = file_get_contents(resource_path() . "/" . "pdf-header.html");
$footer_html = file_get_contents(resource_path() . "/" . "pdf-footer.html");

$pdf = SnappyPDF::loadView('home.pdf');
$pdf->setOption('header-html', $header_html);
$pdf->setOption('footer-html', $footer_html);

return $pdf->stream('invoice.pdf');

You have to create two file named "pdf-header.html" and "pdf-footer.html" in your "resource" folder as below.

pdf-header.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body style="border: 1px solid blue;margin-left: 18px;margin-right: 0px;padding:10px">
<div style="color: red">
    THIS IS <b>PAGE HEADER</b>
</div>
</body>

</html>

pdf-footer.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body style="border: 1px solid blue;margin-left: 18px;margin-right: 0px;padding:10px">
<div style="color: red">
    THIS IS <b>PAGE FOOTER</b>
    Page=<span id="page"></span>|
    Total Page=<span id="topage"></span>|
    Date=<span id="date"></span>|
    Time=<span id="time"></span>
</div>
<script type="text/javascript">
    var vars={};
    var x=window.location.search.substring(1).split('&');
    for (var i in x) {
        var z=x[i].split('=',2);
        vars[z[0]] = unescape(z[1]);
    }
    document.getElementById('page').innerHTML = vars.page;
    document.getElementById('topage').innerHTML = vars.topage;
    document.getElementById('date').innerHTML = vars.date;
    document.getElementById('time').innerHTML = vars.time;
</script>
</body>

</html>




PDF will look like below images:




Tuesday, November 8, 2016

Reload refresh current url or redirect to another url using php script

Refresh/reload current page:
header("Refresh:0");

Or you can redirected to another page using:
header("Refresh:0; url=another-page.php");