2017-06-13 70 views
0

我使用https://phantomjscloud.com生成網站的pdf快照。我使用的是一個非常基本的示例,如下所示:phantomjs API錯誤

$Foptions = new stdClass(); 
$Foptions->url = 'http://lr.boatsetter.com/boat/?id=1'; 
$Foptions->renderType = "pdf"; 

$optionsjson = json_encode($Foptions); 

$url = 'http://PhantomJScloud.com/api/browser/v2/myapi/'; 
$payload = $optionsjson; 
$options = array(
    'http' => array(
     'header' => "Content-type: application/json\r\n", 
     'method' => 'POST', 
     'content' => $payload 
    ) 
); 
$context = stream_context_create($options); 
$result = file_get_contents($url, false, $context); 
if ($result === FALSE) { /* Handle error */ } 
var_dump($result); 
file_put_contents('boat_1.pdf',$result); 

當$ Foptions-> url爲amazon.com或google.com時,此功能完美無缺。但不在我的例子。什麼可能是錯誤?

回答

0

我使用的HTTP API和它的工作:

$headers = array("Accept:"); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

$wp_upload_dir = wp_upload_dir(); 
$upload_dir = $wp_upload_dir['basedir'] . '/pdf/'; 
$url = 'https://phantomjscloud.com/api/browser/v2/myapi/?request={url:%22'.get_home_url().'/boat/?id='.$boat_id.'%22,renderType:%22pdf%22}'; 
$path = $upload_dir."boat_".$boat_id.".pdf"; 

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_REFERER, $url); 

$data = curl_exec($ch); 

curl_close($ch); 

$result = file_put_contents($path, $data); 
    if(!$result){ 
     echo "error"; 
    }else{ 
     echo "success"; 
    } 

$file = $path; 
$filename = 'Boat_'.$boat_id.'.pdf'; 
header('Content-type: application/pdf'); 
header('Content-Disposition: inline; filename="' . $filename . '"'); 
header('Content-Transfer-Encoding: binary'); 
header('Content-Length: ' . filesize($file)); 
header('Accept-Ranges: bytes'); 

@readfile($file);