Mp4 files codec not found - php

I create a script to download video from remote host the content length of file is shows correct and download properly without any interruptions. But When playing video file it shows error The Player might not support the file type or might not support the codec that was used to compress the file.
Here is my php code
ini_set('max_execution_time', 0);
$useragent = "Mozilla/5.0 (X11; Linux amd64; rv:21.0) Gecko/20100101 Firefox/21.0";
$url=($_GET['q']);
$headers = array(
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding: gzip, deflate, br",
"Accept-Charset: utf-8;q=0.7,*;q=0.3",
"Accept-Language:en-US;q=0.6,en;q=0.4",
"Connection: keep-alive",
);
$v = $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 222222);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $v);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$info = curl_exec($ch);
$size2 = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
$namer= basename($v,"=m18");
header('Connection: Keep-Alive');
header('Content-Type: video/mp4; charset=');
header('Content-Transfer-Encoding: binary\n');
header('Content-Description: File Transfer');
header('Accept-Ranges: bytes');
header('Content-Length: ' . $size2);
header('Expires: 0');
header('Cache-Control: private');
header('Pragma: no-cache');
//header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Content-Disposition: inline; filename=");
echo($v);

Related

download multiple files via curl

I have an array of many IDs.
Each ID is assigned to an invoice.
With this curl command I can start download the invoice as pdf:
// DOWNLOAD INVOICE
$curl = curl_init('https://app.invoiz.de/api/invoice/1234/download');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_USERPWD, $apiKey . ":" . $secretApiKey);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$token->token,
'accept: application/pdf'
));
$content = curl_exec($curl);
curl_close($curl);
header('Content-Type: application/pdf');
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"Invoice 1234.pdf\"");
echo $content;
Now I would like to download all invoices as seperated pdf files and I tried this:
foreach ($ids as $id) {
// DOWNLOAD INVOICE
$curl = curl_init('https://app.invoiz.de/api/invoice/'.$id.'/download');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_USERPWD, $apiKey . ":" . $secretApiKey);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$token->token,
'accept: application/pdf'
));
$content = curl_exec($curl);
curl_close($curl);
header('Content-Type: application/pdf');
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"Invoice".$id.".pdf\"");
echo $content;
}
Problem: This will generate only one pdf file with the last invoice ID from the loop.
Where is my mistake?

PHP stream remote video file

Let's say I have a video on a remote server and this its url
"http://domain/video-path/video.mp4"
What is the correct way to stream this video using php with seekable..
I know how to stream the video using fopen and fread but it can't seek with remote file
I'm using this, working both local & external video, seekable & resumable
ini_set('max_execution_time', 0);
$useragent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36";
$v = $_GET['url'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 222222);
curl_setopt($ch, CURLOPT_URL, $v);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$info = curl_exec($ch);
$size2 = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
header("Content-Type: video/mp4");
$filesize = $size2;
$offset = 0;
$length = $filesize;
if (isset($_SERVER['HTTP_RANGE'])) {
$partialContent = "true";
preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches);
$offset = intval($matches[1]);
$length = $size2 - $offset - 1;
} else {
$partialContent = "false";
}
if ($partialContent == "true") {
header('HTTP/1.1 206 Partial Content');
header('Accept-Ranges: bytes');
header('Content-Range: bytes '.$offset.
'-'.($offset + $length).
'/'.$filesize);
header("Content-length: ".$length); // added
} else {
header('Accept-Ranges: bytes');
header("Content-length: ".$size2); // added
}
// header("Content-length: ".$size2); // removed
$ch = curl_init();
if (isset($_SERVER['HTTP_RANGE'])) {
// if the HTTP_RANGE header is set we're dealing with partial content
$partialContent = true;
// find the requested range
// this might be too simplistic, apparently the client can request
// multiple ranges, which can become pretty complex, so ignore it for now
preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches);
$offset = intval($matches[1]);
$length = $filesize - $offset - 1;
$headers = array(
'Range: bytes='.$offset.
'-'.($offset + $length).
''
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 222222);
curl_setopt($ch, CURLOPT_URL, $v);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_exec($ch);
After the other answers on this hadn't been working for me, I've finally found (after much tweaking) a new solution to this which is working for remote/local files.
<?php
$file = 'https://yourfilelocation';
$head = array_change_key_case(get_headers($file, TRUE));
$size = $head['content-length']; // because filesize() won't work remotely
header('Content-Type: video/mp4');
header('Accept-Ranges: bytes');
header('Content-Disposition: inline');
header('Content-Length:'.$size);
readfile($file);
exit;
?>
This allows tracking etc. too, enjoy!

How to retrieve the document from application/octet-stream data in php curl response?

I am using php CURL function to retrieve document from my server . In my curl operation i am getting the response as application/octet-stream data .How can i retrieve the document from bellow response? .
--uuid:9685d4ea-5f71-47d2-938b-04aa830bfc80
Content-Type: application/xop+xml; charset=UTF-8; type="application/soap+xml";
Content-Transfer-Encoding: binary
Content-ID: <root.message#cxf.apache.org>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Header/><soap:Body><ns17:RetrieveDocumentSetResponse xmlns:ns10="http://docs.oasis-open.org/wsrf/bf-2" xmlns:ns11="http://docs.oasis-open.org/wsn/t-1" xmlns:ns12="urn:gov:hhs:fha:nhinc:common:subscriptionb2overridefordocuments" xmlns:ns13="http://www.hhs.gov/healthit/nhin/cdc" xmlns:ns14="urn:gov:hhs:fha:nhinc:common:subscriptionb2overrideforcdc" xmlns:ns15="http://nhinc.services.com/schema/auditmessage" xmlns:ns16="urn:oasis:names:tc:emergency:EDXL:DE:1.0" xmlns:ns17="urn:ihe:iti:xds-b:2007" xmlns:ns18="http://www.caqh.org/SOAP/WSDL/CORERule2.2.0.xsd" xmlns:ns2="urn:gov:hhs:fha:nhinc:common:nhinccommon" xmlns:ns3="urn:gov:hhs:fha:nhinc:common:nhinccommonentity" xmlns:ns4="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0" xmlns:ns5="urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0" xmlns:ns6="urn:oasis:names:tc:ebxml-regrep:xsd:query:3.0" xmlns:ns7="urn:oasis:names:tc:ebxml-regrep:xsd:lcm:3.0" xmlns:ns8="http://www.w3.org/2005/08/addressing" xmlns:ns9="http://docs.oasis-open.org/wsn/b-2"><ns5:RegistryResponse status="urn:oasis:names:tc:ebxml-regrep:ResponseStatusType:Success"/><ns17:DocumentResponse><ns17:HomeCommunityId>urn:oid:2.16.840.1.113883.3.1259.10.1003</ns17:HomeCommunityId><ns17:RepositoryUniqueId>1</ns17:RepositoryUniqueId><ns17:DocumentUniqueId>1.RI0004.000000002.D-1</ns17:DocumentUniqueId><ns17:mimeType>text/xml</ns17:mimeType><ns17:Document><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:ac0edf1f-3243-43b7-bb59-1beaf4392ebc-24#urn%3Aihe%3Aiti%3Axds-b%3A2007"/></ns17:Document></ns17:DocumentResponse></ns17:RetrieveDocumentSetResponse></soap:Body></soap:Envelope>
--uuid:9685d4ea-5f71-47d2-938b-04aa830bfc80
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-ID: <ac0edf1f-3243-43b7-bb59-1beaf4392ebc-24#urn:ihe:iti:xds-b:2007>
P-000000002
D-000000002.1
--uuid:9685d4ea-5f71-47d2-938b-04aa830bfc80--
My code for curl operation is
$body = file_get_contents(dirname(__FILE__).'/doc_retrieve.xml');
$headers = array(
'Content-Type: text/xml; charset="utf-8"',
'Content-Length: '.strlen($body),
'Accept: text/xml',
'Cache-Control: no-cache',
'Pragma: no-cache',
'SOAPAction: "RespondingGateway_CrossGatewayRetrieve"'
);
$ch = curl_init("http://testserver:8080/Gateway/DocumentRetrieve/3_0/EntityService/EntityDocRetrieve?wsdl");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
// Stuff I have added
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$response = curl_exec($ch);
$filename=time();
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Content-Encoding: none');
header('Content-Type: application/.dat');
header('Content-Disposition: attachment; filename='.$filename.'.dat');
$destination = dirname(__FILE__) . '/'.$filename.'.dat';
$file = fopen($destination, "w+");
fputs($file, $response);
fclose($file);
readfile($destination);
die();
Is there any mistake with my code?. Please help .

Allow to download PDF file get(as a attachment) in SOAP API response(PHP)

Calling an SOAP API and its is giving response with XML(added below) and one attachment(PDF in binary in body not in header). What i want is to download that PDF file when getting response. Getting attachment when i call API in SOAP UI tool.
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header/>
<env:Body>
<ns2:response xmlns:ns2="http://url.com/"/>
</env:Body>
</env:Envelope>
When i do print_r for response getting below response as a result
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Powered-By: Servlet
Content-Type: multipart/related; type="text/xml"; start="<#.org>"; boundary="----
=_Part_16"
Transfer-Encoding: chunked
Date:
------=_Part_
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: 8bit
Content-ID: <r.............>
<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>
<env:Header></env:Header>
<env:Body>
<ns2:Response xmlns:ns2="http://url.com/"/></env:Body>
</env:Envelope>
------=_Part_156_1310882897.1451652608850
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-Id: CR/DEF000000000000000000000785_1
%PDF-1.4
...............
PDF content
...............
I have removed some of the value for security reasons.
Passing Header parameters as below
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: application/pdf",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: $API_URL",
"Content-length: ".strlen($request_xml),
);
and passing curl options as below
$ch = curl_init($API_URL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$request_xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec($ch);
Is there any thing wrong or how i can parse data from response body as a PDF?
Just pass your response in PDF file if you have get to a response with binary in the body.
$ch = curl_init($API_URL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$request_xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec($ch);
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Content-Encoding: none');
header('Content-Type: application/.pdf');
header('Content-Disposition: attachment; filename='.$filename.'.pdf');
$destination = dirname(__FILE__) . '/'.$filename.'.pdf';
$file = fopen($destination, "w+");
fputs($file, $response);
fclose($file);
readfile($destination);

CSV Export returning index.php not able to download instead of open CSV file

I have an employee record calling from another server using crul, process the result and create a csv file and then offer it to download or open. but when i clicked to open it opens the csv file .html extension in browser and when i clicked on save option it says "Unable to download the index.php file from sitename.com".
Here is my php code for it:
$url = 'sitename.com/myemployee.php?mid=101';
$headers[] = 'Content-type: Content-type: text/csv; charset=utf-8';
$process = curl_init();
curl_setopt($process, CURLOPT_URL, $url);
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_REFERER, "My_employee");
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
$return = curl_exec($process);
curl_close($process);
header('Content-type: text/csv');
header('Cache-Control: cache, must-revalidate');
header('Pragma: public');
header('Expires: Sat, 6 May 1995 12:00:00 GMT');
header('Content-Disposition: attachment; filename=MyEmployeesReport.csv');
echo $return;
I am using Windows7 and IE 9.
Thanks

Categories