2013-10-14 5 views
0

나는 다음과 같은 코드를 가지고 : 나는 기존 목록의 이름과 내가 원하는 이메일 주소의 배열을 addEmailsToRecipientList을 통과하면PHP- 오류를 반환 500

static function getContext($data) { 
    // use key 'http' even if you send the request to https://... 
    $options = array (
     'http' => array (
      'header' => "Content-type: application/x-www-form-urlencoded\r\n", 
      'method' => 'POST', 
      'content' => http_build_query ($data) 
     ) 
    ); 

    return stream_context_create ($options); 
} 

static function addEmailsToRecipientList($name, $emails) { 

    $url = 'https://sendgrid.com/api/newsletter/lists/email/add.json'; 

    $temp = array(); 
    foreach($emails as $email){ 
     $temp[] = array('email' => $email, 'name' => 'unknown'); 
    } 

    $data = array (
      'list' => $name, 
      'data' => json_encode($temp), 
      'api_user' => $api_user_name, 
      'api_key' => $api_password 
    ); 

    $context = SendGridAPI::getContext ($data); 
    return file_get_contents ($url, false, $context); 
} 

을 그것에 추가하면 오류 500 (내부 서버 오류)이 발생합니다.

하나의 이메일 ($ temp = array ('email'=> $ email, 'name'=> 'unknown'))을 추가하면 정상적으로 작동합니다. 내가 뭘 잘못하고 있니?

고맙습니다.

+0

500 내부 서버 오류 (또는 빈 페이지)는 스크립트에 오류가 발생했지만 오류 메시지를 표시하도록 PHP를 구성하지 않았 음을 의미합니다. 그것은 당신이 더 나아 가기 전에 고칠 필요가있는 것입니다; 오류 메시지의 도움없이 코딩하는 것은 불가능합니다. 여기에 [간단한 설명] (http://stackoverflow.com/a/5680885/13508)이 있습니다. –

+0

API 문서에 "수신자 목록에 하나 이상의 이메일 추가"여러 이메일을 추가하는 것이 지원되지 않을 수 있습니까? http://community.sendgrid.com/sendgrid/topics/when_adding_recipients_i_get_a_database_error_has_occurred – YOKA

+0

정확한 세부 정보가 포함 된 오류 메시지가 표시되지 않으므로 아무 것도 표시되지 않습니다. –

답변

3

해결했습니다! :)

//listname: the name of an existing recipients list 
//$emails: an array of emails 
static function addEmailsToRecipientList($listname, $emails){ 
    $username= 'sendgrid_username'; 
    $password= 'sendgrid_password'; 

    $url = 'https://sendgrid.com/api/newsletter/lists/email/add.json?api_user='.$username.'&api_key='.$password; 

    $str= '&list=' . $listname; 
    for ($i=0;$i<count($emails);$i++) { 
     $str.= '&data[]={"email":"'.$emails[$i] . '","name":"unknown'. $i .'"}'; 
    } 

    return file_get_contents($url . $str); 
} 
0

일부 터치가있는 스크립트는 아주 잘 작동합니다. urlencode, json_decode 및 시뮬레이트 된 이름을 이메일에 추가합니다.

$url = 'https://sendgrid.com/api/newsletter/lists/email/add.json?api_user='.$username.'&api_key='.$password; 

    $str= '&list=' . $listname; 
    foreach ($data as $email) { 
     $attributes = explode("@", $email); 
     $str.= '&data[]='; 
     $str.= urlencode('{"email":"'. $email . '","name":"'. ucfirst($attributes[0]) .'"}'); 
    } 

    $results = file_get_contents($url . $str); 
    $results = json_decode($results, TRUE); 
    return (isset($results['inserted'])) ? $results['inserted'] : 0; 
관련 문제