Я хотел бы опубликовать ответ, который также использует curl для получения содержимого, и mpdf , чтобы сохранить результаты в pdf, поэтому вы получите все шаги типичного варианта использования.
// import mpdf somewhere
require_once dirname(__FILE__) . '/mpdf/vendor/autoload.php';
// get mpdf instance
$mpdf = new \Mpdf\Mpdf();
// src php file
$mysrcfile = 'http://www.somesite.com/somedir/mysrcfile.php';
// where we want to save the pdf
$mydestination = 'http://www.somesite.com/somedir/mypdffile.pdf';
// encode $_POST data to json
$json = json_encode($_POST);
// init curl > pass the url of the php file we want to pass
// data to and then print out to pdf
$ch = curl_init($mysrcfile);
// tell not to echo the results
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1 );
// set the proper headers
curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Content-Length: ' . strlen($json) ]);
// pass the json data to $mysrcfile
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
// exec curl and save results
$html = curl_exec($ch);
curl_close($ch);
// parse html and then save to a pdf file
$mpdf->WriteHTML($html);
$this->mpdf->Output($mydestination, \Mpdf\Output\Destination::FILE);
В $ mysrcfile я буду читать данные json, подобные этому (как указано в предыдущих ответах):
$data = json_decode(file_get_contents('php://input'));
// (then process it and build the page source)
Просто нужно выполнить приведение перед вызовом метода. return ((MyValue) t).getValue();
, так что компилятор может знать, что он вызывает метод MyValue.
static <T> int getValue(T t) {
return ((MyValue) t).getValue();
}
в случае нескольких классов вы можете проверить наличие экземпляров, используя оператор instanceof
, и вызвать метод .. как показано ниже
static <T> int getValue(T t) {
//check for instances
if (t instanceof MyValue) {
return ((MyValue) t).getValue();
}
//check for your other instance
return 0; // whatever for your else case.
Компилятор не знает, что вы собираетесь передать getValue()
экземпляр класса с методом getValue()
, поэтому t.getValue()
не проходит компиляцию.
Он узнает об этом, только если вы добавите тип, связанный с параметром универсального типа T
:
static <T extends MyValue> int getValue(T t) {
return t.getValue();
}
Конечно, в таком простом примере вы можете просто удалить параметр универсального типа и написать:
static int getValue(MyValue t) {
return t.getValue();
}