download multiple files via curl - php

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?

Related

Mp4 files codec not found

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);

how to implement soap request in php?

I searched this website for a helpful response for integrating a SOAP Request in PHP, but I didn't find anything that solved my problem.
I'm new to SOAP and can't figure out why I am not getting any response:
<?php
$xml = '<?xml version="1.0" encoding="utf-8"?>'.
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'.
' xmlns:xsd="http://www.w3.org/2001/XMLSchema"'.
' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'.
'<soap:Body>'.
'<food xmlns="http://www.localhost:81/dbWIP/xml1.xml">'.
'<price>$5.95</price>'.
'</food>'.
'</soap:Body>'.
'</soap:Envelope>';
$url = "http://www.localhost:81/dbWIP/xml1.xml";
$ch = curl_init();
echo $ch;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$headers = array();
array_push($headers, "Content-Type: text/xml; charset=utf-8");
array_push($headers, "Accept: text/xml");
array_push($headers, "Cache-Control: no-cache");
array_push($headers, "Pragma: no-cache");
array_push($headers, "SOAPAction:http://www.localhost:81/dbWIP/xml1.xml");
if($xml != null) {
echo $xml;
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml");
array_push($headers, "Content-Length: " . strlen($xml));
}
curl_setopt($ch, CURLOPT_USERPWD, "user_name:password"); /* If required */
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
echo $response;
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo"<br/> CODE:"$code;
curl_close($ch);
?>
I think you are missing URL from curl_init which will initialize a cURL session, but yours has no URL.
$curl = curl_init("http://www.localhost:81/dbWIP/xml1.xml");
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
//curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
And try setting your headers to
$headers = array(
'Content-type: text/xml;charset="utf-8"',
'Accept: text/xml',
'Cache-Control: no-cache',
'Pragma: no-cache',
'Content-length: ' . strlen($xml),
);
Try to remove the quotation marks:
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);

change content-type HTTP header google drive file

I want to proxy a file and use that header with php: header("Content-Type: application/octet-stream");
My code:
<?php
$id = '0B475ByfcR9n4a1JMVEZxQno2Tmc';
$ch = curl_init('https://drive.google.com/uc?id='.$id.'&export=download');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, []);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
$object = json_decode(str_replace(')]}\'', '', $result));
exit(header('Location: '. $object->downloadUrl));
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment;)
What should i do? Please help me.
You can't simply add a header redirect, along with the additional headers. When the browser sees the location header, it will move to the new location and read those headers from the response. You'll need to output the file directly from google drive (Which requires the server to download the file, and relay it to the client. Something like so):
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment;');
$ch2 = curl_init($object->downloadUrl);
curl_setopt($ch2, CURLOPT_POSTFIELDS, []);
curl_setopt($ch2, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_exec($ch2);

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 .

Send byte data in the request

Hi I am having trouble sending only byte data in a request
I only want to sent the [bytes from data stream] but am struglling in stripping this from my curl request.
Can anybody help me stripping this of via curl please.
-----------------------------7dc1f42e3005a8
Content-Disposition: form-data; name="upload"; filename="file.mp3"
Content-Type: audio/mpeg
**[bytes from data stream]**
-----------------------------7dc1f42e3005a8--
The curl that I am using below, I am wondering if there is a quick way in curl to send only a specific part of load in this case only the byte data.
$fp = fopen($localfile['tmp_name'], 'r');
$ch = curl_init();
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); // --data-binary
curl_setopt($ch, CURLOPT_URL, 'http://api.com/api/v4/track/upload?api_key=xxx&filetype=mp3');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/octet-stream'));
$response = curl_exec($ch);
you can use below solution
//your url
$url = 'http://localhost/test/test.php';
$file = realpath('php1.php');
// build multipart
//set appropriate content type in headers
$params = "--ABC1234\r\n"
. "Content-Type: application/x-www-form-urlencoded\r\n"
. "--ABC1234\r\n"
. "Content-Type: text/php\r\n"
. "Content-Disposition: attachment; filename=\"php1.php\"\r\n"
. "\r\n"
. file_get_contents($file) . "\r\n"
. "--ABC1234--";
$first_newline = strpos($params, "\r\n");
$multipart_boundary = substr($params, 2, $first_newline - 2);
$request_headers = array();
$request_headers[] = 'Content-Length: ' . strlen($params);
$request_headers[] = 'Content-Type: multipart/form-data; boundary=' . $multipart_boundary;
// send the request now
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
$reply = curl_exec($ch);
curl_close ($ch);
on other end you will be able to get uploaded file in $_FILES variable.
Tested at local server.
Hope this will help you.

Categories