2012-06-13 3 views
0

이 코드를 사용하여 C2DM 푸시 메시징의 일부인 응용 프로그램 서버를 설정하려고합니다 (https://github.com/lytsing/c2dm-php).PHP - C2DM 응용 프로그램 서버 구현

애플 리케이션 측면을 완료하고 Google에 이메일 주소를 등록했습니다. php/cURL이 설치된 서버에서 코드를 실행할 때마다 'get auth token error'오류가 발생합니다. 문제 해결을 위해 어디서부터 시작해야할지 모르겠다. s2dm.php 파일에 - -

내가 코드에서 변경된 유일한 선은

'source'  => 'com.phonegap.chillimusicapp', 

와 나는 post.php 파일로 내 이메일/비밀번호를 추가 -

$result = $c2dm->getAuthToken("[email protected]", "password"); 

어떤 충고라도 좋을 것입니다! 건배 폴

답변

1

아래의 샘플 코드를 사용해보세요. 정상적으로 작동하고 있습니다.

<?php 
define("C2DM_ACCOUNT_EMAIL","[C2DM_EMAIL]"); 
define("C2DM_ACCOUNT_PASSWORD","[C2DM_PASSWORD]"); 
define("C2DM_CLIENT_LOGIN_URL","https://www.google.com/accounts/ClientLogin"); 
define("C2DM_MSG_SEND_URL","https://android.apis.google.com/c2dm/send"); 

function sendPushNotification($device_reg_id,$msg){ 

    $auth_id=get_auth_id(); // To get Auth ID 

    $post_fields=array(
     'collapse_key=ck_1', 
     'registration_id='. trim($device_reg_id), 
     'data.payload='. trim($msg), 
    ); 

    $data_str=implode('&', $post_fields); 

    $headers = array(
    'Authorization: GoogleLogin auth='.trim($auth_id), 
    'Content-Type: application/x-www-form-urlencoded', 
    'Content-Length: '.trim(strlen($data_str)), 
    'Connection: close' 
    ); 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,C2DM_MSG_SEND_URL); 
    curl_setopt($ch,CURLOPT_HTTPHEADER,$headers); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_str); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $server_output = curl_exec ($ch); 
    curl_close ($ch); 
// print_r($server_output); 
} 

function get_auth_id(){ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,C2DM_CLIENT_LOGIN_URL); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, "Email=".C2DM_ACCOUNT_EMAIL."&Passwd=".C2DM_ACCOUNT_PASSWORD."&accountType=GOOGLE&source=Google-cURL-Example&service=ac2dm"); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $server_output = curl_exec ($ch); 
    curl_close ($ch); 
// print_r($server_output); 
    $parts=explode("Auth=",$server_output); 
    $auth_id=$parts[1]; 
// echo $auth_id; 
    return $auth_id; 
} 

$reg_id = "[DEVICE_REG_ID]"; 
sendPushNotification($reg_id,"Hello World...!! Jay is testing C2DM..."); 

참고하시기 바랍니다. 알림을 보낼 때마다 get_auth_id()를 호출 할 필요가 없습니다. 한 번 호출하고 auth_id를 구성 파일의 어딘가에 저장할 수도 있습니다.

+0

안녕하세요 Jay - 정말 고마워요. 이전 솔루션을 완전히 대체 한 것입니까? – Dancer

+0

메시지 전송을 위해 보호 된 양식을 사용하여이를 연결하는 일반적인 방법이 있습니까? – Dancer

+0

예 Paul, 해결책을 대신 할 것입니다. 그러나 이것이 당신의 필요를 정확히 충족시킬 것이라고 생각합니까? – jaym