Задача: в веб интерфейс сайта вывести кнопку получения файла со стороны 1С
Решение:
На стороне 1с оформим сервис по примеру:
HTTPОтвет = Новый HTTPСервисОтвет(200);
ЗаписьЖурналаРегистрации("Пришло в HTTP.имя файла", УровеньЖурналаРегистрации.Информация,,имяфайла,имяфайла+"!",);
ДВ = Новый ДвоичныеДанные(имяфайла);
HTTPОтвет.УстановитьТелоИзДвоичныхДанных(ДВ);
HTTPОтвет.Заголовки["Content-Description"]="File Transfer";
HTTPОтвет.Заголовки["Pragma"]="public";
HTTPОтвет.Заголовки["Expires"]="0";
HTTPОтвет.Заголовки["Cache-Control"]="must-revalidate, post-check=0, pre-check=";
HTTPОтвет.Заголовки["Cache-Control"]="public";
HTTPОтвет.Заголовки["Content-Type"]="text/plain; charset=UTF-8";
HTTPОтвет.Заголовки["Content-Type"] = "application/invoice.pdf";
HTTPОтвет.Заголовки["Content-Disposition"] = "attachment; filename=chet.pdf";
Возврат HTTPОтвет;
На стороне сайта, можно оформить в виде:
class TApi1c {
public $url="";
public $login="";
public $password="";
public function __construct($url,$login,$password) {
$this->url=$url;
$this->login=$login;
$this->password=$password;
}
public function reqwest($reqwest,$body=array()){
if (LK_DEBUG==true):
$data=Date("m-d-y h:i:s")." - посылаем в подсистему 1C $reqwest :\n";
$data=$data.json_encode($body)."\n";
file_put_contents(API_LOG_FILE, $data,FILE_APPEND);
endif;
$ch = curl_init($this->url);
curl_setopt($ch, CURLOPT_USERPWD, $this->login.":".$this->password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$js["reqwest"]=$reqwest;
$js["body"]=$body;
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($js));
//curl_setopt($ch, CURLOPT_TIMEOUT, 300);
$res=curl_exec($ch);
if (LK_DEBUG==true):
$data=Date("m-d-y h:i:s")." - сырой ответ :\n";
$data=$data.serialize($res)."\n";
file_put_contents(API_LOG_FILE, $data,FILE_APPEND);
endif;
//echo "!!";
//var_dump($res);
//echo "!!";
$response = json_decode($res);
if ($response==null){
AddErrorMessage("Ошибка","Внутрення ошибка сервера. Попробуйте позже.");
if (LK_DEBUG==true):
file_put_contents(API_LOG_FILE, $data,FILE_APPEND);
endif;
};
return $response;
if(curl_errno($ch)){
throw new Exception(curl_error($ch));
};
}
}
class TInvoice {
public $api=""; // класс работы с API
public function __construct($api){
$this->api=$api;
}
public function GetInvoice($hash){
$req["hash"]=$hash;
$res=$this->api->naked_reqwest("GetInvoice",$req);
if ($res!=null){
return $res;
} else {
$res = new \stdClass();
$res->error=true;
$res->result="Сервер не доступен. Попробуйте позже.";
AddErrorMessage("Ошибка",$res->result);
};
return $res;
}
}
$Api1c=new TApi1c(url_1c,user_1c,password_1c);
if (isset($_GET["id"])==false){$_GET["id"]="";};
$id=$_GET["id"];
$inv=new TInvoice($Api1c);
$res=$inv->GetInvoice($id);
if (($res!="error") or ($res!="notfound")){
header('Content-type: application/pdf');
header("Content-Disposition: attachment; filename=invoice.pdf");
header("Pragma: no-cache");
};
echo $res;