PHP: Downloading pdf file without direct link [closed] - downloading-website-files

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to download pdf file from this url:
http://knihy.cpress.cz/?p=actions&action=download/file&value=files&id=149253
I tried get file through file_get_contents, but it downloaded just php file without actual pdf.
Is there a way to download this file?
Thank you very much!

<?php
function collect_file($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_REFERER, "http://www.xcontest.org");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
return($result);
}
function write_to_file($text,$new_filename){
$fp = fopen($new_filename, 'w');
fwrite($fp, $text);
fclose($fp);
}
// start loop here
$new_file_name = "testfile.pdf";
$url = "http://knihy.cpress.cz/?p=actions&action=download/file&value=files&id=149253";
$temp_file_contents = collect_file($url);
write_to_file($temp_file_contents,$new_file_name)
// end loop here
?>
from here

Related

Upload file to JIRA REST API with PHP cUrl

I want upload a file to JIRA with REST API.
My code :
$ch_api = curl_init('https://website.com/rest/api/2/issue/SI-10255/attachments');
curl_setopt($ch_api, CURLOPT_POST, true);
curl_setopt($ch_api, CURLOPT_POSTFIELDS, json_encode(["file" => "#t.txt"]));
curl_setopt($ch_api, CURLOPT_HTTPHEADER, array('Content-Type:multipart/form-data','X-Atlassian-Token:no-check'));
curl_setopt($ch, CURLOPT_USERPWD, "login:password");
curl_setopt($ch_api, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch_api);
curl_close($ch_api);
And i have this error :
the request was rejected because no multipart boundary was found
And when i add a boundary, the file is not uploaded. The response from api was a blank array.
Please help.
Thanks.
For upload file to JIRA REST API, not use the header "Content-Type:multipart/form-data"
And for the file, use this code :
if (function_exists('curl_file_create')) {
$cFile = curl_file_create("file.txt");
} else {
$cFile = '#' . realpath("file.txt");
}

Paypal IPN CURL returns "SSL connect error"

I am trying to implement ipn in my system but shows error
SSL connect error
TLS version shows TLS 1.0
It is a client's system. I downloaded the code from paypal website. I am currently testing it.
but it throws error
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
$ch = curl_init('https://www.howsmyssl.com/a/check');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$json = json_decode($data);
echo $json->tls_version;
file_put_contents('NAB/paypal-ipn.txt', var_export($_POST, true));
echo "ipn processing";
// STEP 1: read POST data
// Reading POSTed data directly from $_POST causes serialization issues with array data in the POST.
// Instead, read raw POST data from the input stream.
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
$keyval = explode ('=', $keyval);
if (count($keyval) == 2)
$myPost[$keyval[0]] = urldecode($keyval[1]);
}
echo "<br>1";
// read the IPN message sent from PayPal and prepend 'cmd=_notify-validate'
$req = 'cmd=_notify-validate';
if (function_exists('get_magic_quotes_gpc')) {
$get_magic_quotes_exists = true;
}
echo "<br>2";
foreach ($myPost as $key => $value) {
if ($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
$value = urlencode(stripslashes($value));
} else {
$value = urlencode($value);
}
$req .= "&$key=$value";
}
echo "<br>3";
// Step 2: POST IPN data back to PayPal to validate
$ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
//$ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
// In wamp-like environments that do not come bundled with root authority certificates,
// please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set
// the directory path of the certificate as shown below:
// curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
echo "<br>3";
if ( !($res = curl_exec($ch)) ) {
error_log("Got " . curl_error($ch) . " when processing IPN data");
echo curl_error($ch);
curl_close($ch);
//exit;
}
If 'howsmyssl' is reporting that the systems supports only TLS v 1.0, it is likely that an upgrade is necessary to support TLS v 1.2. The curl_setopt($ch, CURLOPT_SSLVERSION, 6); is requesting TLS v 1.2 (see list in PHP Curl (with NSS) is probably using SSLv3 insted of TLS when connecting to https).
Also, as of June 30,2017, PayPal will be requiring TLS v 1.2 (see https://www.paypal-knowledge.com/infocenter/index?page=content&widgetview=true&id=FAQ1914&viewlocale=en_US and Php curl set ssl version).
You may be able to test your integration using TLS v 1.0 (until 6/30/2017) by changing to curl_setopt($ch, CURLOPT_SSLVERSION, 4);.
I had a similar problem problem when implementing the paypal ipn listener code. The script would stop executing where step 2 starts in the code you've provided. I solved it by downloading cacert.pem from http://curl.haxx.se/docs/caextract.html. Then I copied the downloaded file to the same folder as paypalipnlistener.php and then uncommented the following code in paypalipnlistener.php // curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem'); to curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem'); and the script executed successfully.

Pulling metadate from url with php, strange characters?

I'm having a bit of an issue, im trying to pull basic metadata from an external URL, I have successfuly got it to do so for the most part but its causing a few character issues on letters that are Ä ä ö are coming out like mäenjaksa7-300x200.jpg when i call the images url which is actually mäenjaksa7-300x200.jpg, my code is below and thank you for helping.
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data; }
$html = file_get_contents_curl($params['url']);
//parsing begins here:
$doc = new DOMDocument();
#$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');
//get and display what you need:
$urltitle = $nodes->item(0)->nodeValue;
$metas = $doc->getElementsByTagName('meta');
for ($i = 0; $i < $metas->length; $i++)
{
$meta = $metas->item($i);
if($meta->getAttribute('name') == 'description')
$description = $meta->getAttribute('content');
if($meta->getAttribute('name') == 'keywords')
$keywords = $meta->getAttribute('content');
if($meta->getAttribute('property') == 'og:image')
$ogimage = $meta->getAttribute('content');
if($meta->getAttribute('rel') == 'image_src')
$relimage = $meta->getAttribute('content');
}
if( empty($ogimage) ) {
$metaimage = $relimage;
} else {
$metaimage = $ogimage;
}
Perhaps you have to make sure that your url header have content-type -> charset to utf-8 or appropriate one. You have to make sure that your url is not content none Ascii character or make sure you have properly set the appropriate "character’s encoder". Maybe i haven’t well understood your problem, however look at this example which have not relation to your code but can be useful:
$url = "http://www.example.com/services/calculation";
$page = "/services/calculation";
$headers = array(
"POST ".$page." HTTP/1.0",
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"run\"",
"Content-length: ".strlen($xml_data),
"Authorization: Basic " . base64_encode($credentials)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
Solution:
add this below
Find:
$html = file_get_contents_curl($url);
Add beow it:
//Change encoding to UTF-8 from ISO-8859-1
$html = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $html);

Intuit QBMS Error: Unknown error returned by SAX parser

I'm trying to set up a credit card interface on a website using Intuit's QBMS and I've got the XML request set up per the specifications on the Intuit documentation. I've also got my AppID, Connection Ticket, and Application Login set up. But when I do a test, I'm getting the following error:
Unknown error returned by SAX parser. Exception from other package: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
Here is the XML Request:
$productionUrl = 'https://merchantaccount.quickbooks.com/j/AppGateway';
$connectionTicket = 'SDK-xxx-xxx-xxxxxxxxxxxxxxxxxxxxxxxx';
$applicationLogin = 'qbms.site.com';
$appID = "111111111";
$xml = rawurlencode('<?xml version="1.0"?>
<?qbmsxml version="4.1"?>
<QBMSXML>
<SignonMsgsRq>
<SignonDesktopRq>
<ClientDateTime>' . date('Y-m-d\TH:i:s') . '</ClientDateTime>
<ApplicationLogin>' . $applicationLogin . '</ApplicationLogin>
<ConnectionTicket>' . $connectionTicket . '</ConnectionTicket>
<Language>English</Language>
<AppID>' . $appID . '</AppID>
<AppVer>1.0</AppVer>
</SignonDesktopRq>
</SignonMsgsRq>
<QBMSXMLMsgsRq>
<CustomerCreditCardChargeRq>
<TransRequestID>' . $_transId . '</TransRequestID>
<CreditCardNumber>' . $_cc_number . '</CreditCardNumber>
<ExpirationMonth>' . $_cc_exp_month . '</ExpirationMonth>
<ExpirationYear>' . $_cc_exp_year . '</ExpirationYear>
<IsCardPresent>false</IsCardPresent>
<Amount>' . $_amount . '</Amount>
<NameOnCard>' . $_cc_name . '</NameOnCard>
<CreditCardAddress>' . $_cc_address . '</CreditCardAddress>
<CreditCardPostalCode>' . $_cc_zip . '</CreditCardPostalCode>
<SalesTaxAmount>' . $_tax . '</SalesTaxAmount>
</CustomerCreditCardChargeRq>
</QBMSXMLMsgsRq>
</QBMSXML>');
$header[] = "Content-type: application/x-qbmsxml";
$header[] = "Content-length: " . strlen($xml);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, $productionUrl);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$response = curl_exec($ch);
curl_close($ch);
print_r($response);
Since the documentation is still not available, in particular https://developer.intuit.com/qbsdk-current/Common/newOSR/index.html
This is speculation based on my testing. Check to see that the <TransRequestID>
contains a number, with only leading or following alpha characters (if any).
The response contains this xml: <TxnAuthorizationStamp>
which is your TransRequestID incremented by 1. I think if it can't figure out how to increment it will fail.
Please update any progress you have.
I know that this is an old question but I ran into similar problems and was able to get it working.
When I removed this phrase from the request xml packet:
<?xml version="1.0"?>
The SAX parser error went away and it works.

Auto Facebook Login with Curl

I'm trying to autologin to facebook using curl, but nothing will work. The nearest I got was that I logged in in my current window (so I had the normal facebook-homepage on my-server.com/facebooklogin.php), but when I open facebook.com in a new tab I'm not logged in anymore.
Does someone know how I can login into facebook with cURL/PHP or maybe something else?
Code: [Select]
function Send_to_Server($url) {
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'email='.urlencode('MyEmail#Email.com').'&pass='.urlencode('MyPassword').'&login=Login');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER,0);
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_NOBODY, 1);
$result = curl_exec ($ch);
curl_close ($ch);
return $result;
}
$string = Send_to_Server('https://login.facebook.com/login.php?m&next=http%3A%2F%2Fm.facebook.com%2Fhome.php');
echo $string;
That doesn't work :D I don't have any output and I'm not logged in in facebook
Greetings
Not really possible and even if you could find a way to do it it's probably against their TOS. What are you trying to do? Maybe you could create an app and grant yourself offline_access to generate a long lasting token and use that to meet your requirement.
this seems to have what you want...
www.360percents.com/posts/php-curl-status-update-working-example