2014-04-09 3 views
0

이것은 내 "전자 메일 및 양식에 데이터 추가"PHP 백엔드 스크립트에 사용하는 코드입니다. 어떤 이유로 인해 처음 5 개의 셀을 이름으로 변경할 수 없으며, 양식 데이터를 추가하는 데 필요한 전자 메일, 타임 스탬프, 주문, 총 가격. 나는 또한 각각의 명사가 자신의 페이지로 이동하도록 각 이름에 대해 새 장을 만드는 방법을 알아 내려고 노력 중이다. 이 코드 중 일부는 Zend에서 처리하지만 그 프레임 워크는 더 이상 지원되지 않습니다. 나는 내 머리 위로 조금있다. 너희들도 도와 줄 수 있니?사용자 정의 양식으로 Google 스프레드 시트에 작성

// set credentials for ClientLogin authentication 
$user = "My username"; 
    $pass = "My Pass"; 
// set target spreadsheet and worksheet 
$ssKey = 'Order'; 
    $wsKey = '$name'; 

    // update cell at row 1, column X 
$entry = $service->updateCell('1', '1', 'Name', $ssKey, $wsKey); 
$entry = $service->updateCell('1', '2', 'Email', $ssKey, $wsKey); 
$entry = $service->updateCell('1', '3', 'Timestamp', $ssKey, $wsKey); 
$entry = $service->updateCell('1', '4', 'Order', $ssKey, $wsKey); 
$entry = $service->updateCell('1', '5', 'Total Price', $ssKey, $wsKey); 

include 'spreadsheet.php'; 
$Spreadsheet = new Spreadsheet($user, $pass); 
$Spreadsheet-> 
setSpreadsheet($wsKey)-> 
setWorksheet($wsKey)-> 
add(array("Name" => "$name", "Email" => "$visitor_email", "Timestamp" => "$datevalue", "Order" => "$sandwich, $sandwichside, $salads, $beverages", "Total Price" => "\$$TotalPrice")); 

Speadsheet.php은 이것이다 :에

<? 
#from php-form-builder-class 
class Spreadsheet { 
private $token; 
private $spreadsheet; 
private $worksheet; 
private $spreadsheetid; 
private $worksheetid; 

public function __construct($username, $password) { 
    $this->authenticate($username, $password); 
} 

public function authenticate($username, $password) { 
    $url = "https://www.google.com/accounts/ClientLogin"; 
    $fields = array(
     "accountType" => "HOSTED_OR_GOOGLE", 
     "Email" => $username, 
     "Passwd" => $password, 
     "service" => "wise", 
     "source" => "pfbc" 
    ); 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $fields); 
    $response = curl_exec($curl); 
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
    curl_close($curl); 

    if($status == 200) { 
     if(stripos($response, "auth=") !== false) { 
      preg_match("/auth=([a-z0-9_\-]+)/i", $response, $matches); 
      $this->token = $matches[1]; 
     } 
    } 
} 

public function setSpreadsheet($title) { 
    $this->spreadsheet = $title; 
    return $this; 
} 

public function setSpreadsheetId($id) { 
    $this->spreadsheetid = $id; 
    return $this; 
} 

public function setWorksheet($title) { 
    $this->worksheet = $title; 
    return $this; 
} 

public function add($data) { 
    if(!empty($this->token)) { 
     $url = $this->getPostUrl(); 
     if(!empty($url)) { 
      $headers = array(
       "Content-Type: application/atom+xml", 
       "Authorization: GoogleLogin auth=" . $this->token, 
       "GData-Version: 3.0" 
      ); 

      $columnIDs = $this->getColumnIDs(); 

      if($columnIDs) { 
       $fields = '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">'; 
       foreach($data as $key => $value) { 
        $key = $this->formatColumnID($key); 
        if(in_array($key, $columnIDs)) 
         $fields .= "<gsx:$key><![CDATA[$value]]></gsx:$key>"; 
       } 
       $fields .= '</entry>'; 

       $curl = curl_init(); 
       curl_setopt($curl, CURLOPT_URL, $url); 
       curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
       curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
       curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
       curl_setopt($curl, CURLOPT_POST, true); 
       curl_setopt($curl, CURLOPT_POSTFIELDS, $fields); 
       $response = curl_exec($curl); 
       $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
       curl_close($curl); 
      } 
     } 
    } 
} 

private function getColumnIDs() { 
    $url = "https://spreadsheets.google.com/feeds/cells/" . $this->spreadsheetid . "/" . $this->worksheetid . "/private/full?max-row=1"; 
    $headers = array(
     "Authorization: GoogleLogin auth=" . $this->token, 
     "GData-Version: 3.0" 
    ); 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
    $response = curl_exec($curl); 

    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
    curl_close($curl); 

    if($status == 200) { 

     $columnIDs = array(); 
     $xml = simplexml_load_string($response); 
     if($xml->entry) { 
      $columnSize = sizeof($xml->entry); 
      for($c = 0; $c < $columnSize; ++$c) 
       $columnIDs[] = $this->formatColumnID($xml->entry[$c]->content); 
     }    
     return $columnIDs;    
    } 

    return ""; 
} 

private function getPostUrl() { 
    if (empty($this->spreadsheetid)){ 

     #find the id based on the spreadsheet name 

     $url = "https://spreadsheets.google.com/feeds/spreadsheets/private/full?title=" . urlencode($this->spreadsheet); 
     $headers = array(
         "Authorization: GoogleLogin auth=" . $this->token, 
         "GData-Version: 3.0" 
     ); 
     $curl = curl_init(); 
     curl_setopt($curl, CURLOPT_URL, $url); 
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
     $response = curl_exec($curl); 
     $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); 

     if($status == 200) { 
      $spreadsheetXml = simplexml_load_string($response); 
      if($spreadsheetXml->entry) { 
       $this->spreadsheetid = basename(trim($spreadsheetXml->entry[0]->id)); 
       $url = "https://spreadsheets.google.com/feeds/worksheets/" . $this->spreadsheetid . "/private/full";            
       if(!empty($this->worksheet)) 
        $url .= "?title=" . $this->worksheet; 

       curl_setopt($curl, CURLOPT_URL, $url); 
       $response = curl_exec($curl); 
       $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
       if($status == 200) { 
        $worksheetXml = simplexml_load_string($response); 
        if($worksheetXml->entry) 
         $this->worksheetid = basename(trim($worksheetXml->entry[0]->id)); 
       } 
      } 
     } 
     curl_close($curl); 
    } 


    if(!empty($this->spreadsheetid) && !empty($this->worksheetid)) 
     return "https://spreadsheets.google.com/feeds/list/" . $this->spreadsheetid . "/" . $this->worksheetid . "/private/full"; 

    return ""; 
} 

private function formatColumnID($val) { 
    return preg_replace("/[^a-zA-Z0-9.-]/", "", strtolower($val)); 
} 
} 
?> 

답변

0

기능 부착 Speadsheet.php 만 포함 (구글 문서 (U 수동으로 웹 인터페이스를 통해 생성 헤더처럼) preformated 기존에 새로운 값을 추가 + 연결 및 Google 문서에 인증과 같은 일부 초기 작업). 이 spreadsheet.php이 제공하지 않는 an extended 기능을 찾는 것처럼 보입니다 ...

관련 문제