php Api mailchimp

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Api mailchimp相关的知识,希望对你有一定的参考价值。

class MailChimp {

	private $username;
	private $pass;

	private $status_code;
	private $curl_response;

	const apiKey = '261fdbc6602031972b5e5829367d5fa6-us16';
	const url = 'https://us16.api.mailchimp.com/3.0/';

	/**
	 * MailChimp constructor.
	 *
	 * @param $username
	 * @param $pass
	 */
	public function __construct( $username,$pass ) {
		$this->username = $username;
		$this->pass     = $pass;
	}

	/**
	 * Get api url
	 * @return string
	 */
	public function getUrlApi() {
		$dataCenter = substr( $this->pass,strpos( $this->pass,'-' ) + 1 );
		return "https://{$dataCenter}.api.mailchimp.com/3.0/";
	}

	/**
	 * Add Suscriber to list
	 *
	 * @param $email
	 *
	 * @return bool|mixed
	 */
	public function addSuscriberToList( $email ) {

		if(filter_var($email, FILTER_VALIDATE_EMAIL)){
			if ( ! $this->getStatusSuscriber( $email ) ) {

				$datosSuscriptor = [
					'email_address' => $email,
					'status'        => 'subscribed',
					'merge_fields'  => [
						"FNAME" => $email,
						"LNAME" => ""
					]
				];

				$datos = json_encode( $datosSuscriptor );
				$url   = $this->getUrlApi() . '/lists/' . self::getListID() . '/members/' . md5( strtolower( $email ) );
				self::sendCurl( $url,'PUT',$this->username,$this->pass,array(),$datos );

				$msg = 'Thanks for your subscription.';
			} else {
				$msg = 'You are already subscribed.';
			}
		}else{
			$msg="Invalid Email";
		}

		return $msg;
	}

	/**
	 * Send CURL
	 *
	 * @param $url
	 * @param $method
	 * @param $username
	 * @param $password
	 * @param array $parametros
	 *
	 * @param null $post_fields
	 *
	 * @return bool|mixed
	 */
	private function sendCurl( $url,$method,$username,$password,$parametros = array(),$post_fields = null ) {
		//url contra la que atacamos
		$ch = curl_init( $url );

		//a true, obtendremos una respuesta de la url, en otro caso,
		curl_setopt( $ch,CURLOPT_RETURNTRANSFER,true );

		//establecemos el verbo http que queremos utilizar para la petición
		curl_setopt( $ch,CURLOPT_CUSTOMREQUEST,$method );
		//enviamos el array data
		curl_setopt( $ch,CURLOPT_HTTPHEADER,$parametros );

		if ( ! is_null( $post_fields ) ) {
			curl_setopt( $ch,CURLOPT_POSTFIELDS,$post_fields );
		}

		curl_setopt( $ch,CURLOPT_URL,$url );
		curl_setopt( $ch,CURLOPT_USERPWD,"{$username}:{$password}" );
		curl_setopt( $ch,CURLOPT_HTTPAUTH,CURLAUTH_BASIC );
		//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

		//obtenemos la respuesta
		$response          = curl_exec( $ch );
		$this->status_code = curl_getinfo( $ch,CURLINFO_HTTP_CODE );

		// Se cierra el recurso CURL y se liberan los recursos del sistema
		curl_close( $ch );

		$this->curl_response = ! $response ? false : json_decode( $response,true );

		//Retornar los valores
		return ! $response ? false : json_decode( $response,true );
	}

	/**
	 * Get first id list
	 * @return mixed
	 */
	public function getListID() {
		$listid = self::sendCurl( $this->getUrlApi() . '/lists','GET',$this->username,$this->pass );
		return $listid['lists'][0]['id'];
	}

	/**
	 * Get Status Suscriber
	 *
	 * @param $mail
	 *
	 * @return bool
	 */
	public function getStatusSuscriber( $mail ) {
		$mail   = md5( strtolower( $mail ) );
		$url    = self::getUrlApi() . '/lists/' . self::getListID() . '/members/' . $mail;
		$status = self::sendCurl( $url,'GET',$this->username,$this->pass );

		return $status['status'] == 404 ? 0 : 1;
	}

}

以上是关于php Api mailchimp的主要内容,如果未能解决你的问题,请参考以下文章

Mailchimp API PHP:500 错误

php Api mailchimp

将查询参数添加到 PHP 的 Mailchimp API 请求

如何使用 PHP 列表活动在 Mailchimp Marketing API 中设置计数

通过 Curl (Mailchimp API) 发布 PHP 会话

无法创建活动 Mailchimp API 3.0 - PHP