이전에 보낸 전자 메일이 반송 될 때 보낸 사람에게 전자 메일을 보낼 Mandrill 용 Webhook을 만들려고합니다. 나는 드릴에서 JSON 데이터를 수신 할 수 있어요,하지만 데이터가 원래의 보낸 사람에게 보낼 수 있다는 분석 드릴 수 없습니다 :PHP에서 대괄호로 인해 JSON 필드를 구문 분석 할 수 없습니다.
여기<?php
require_once 'mandrill-api-php/src/Mandrill.php'; //Not required with Composer
$mandrill = new Mandrill('*myapikey*');
$json = stripslashes($_POST['mandrill_events']);
$jsondata = json_decode($json,true);
$subject = $jsondata['event'];
$message = "STRIPSLASHES: ".$json."----JSONDATA----".$jsondata;
$emailAddress = "*[email protected]*";
mail($emailAddress, $subject, $message);
?>
가 $의 JSON 데이터가 $ 메시지 변수의 모습입니다. 그것은 문자 그대로의 사본 내가받은 테스트 전자 메일에서 붙여 넣기 :
위해서 stripslashes : [{ "이벤트": "스팸 메일", "MSG": { "TS": 1365109999, "제목" " 이 예제 웹 훅 메시지 ","email ":"[email protected] ","sender ":"[email protected] ","tags ": ["webhook-example "],"opens " : "{" ": 1365111111}", "클릭": [{ ""t : "1365111111,"url ":"http://mandrill.com "}]"상태 ":"보낸 ","메타 데이터 " : "example idaaaaaaaaaaaaaaaaaaaaaaaa", "_ version": "exampleaaaaaaaaaaaaaaa"} "_ id": "exampleaaaaaaaaaaaaaaaaaaaaaaaaa", "ts": 1422475458} {{event} : "스팸", { "msg": { "ts": 1365109999, "subject": "이 예제 웹 훅 메시지", "email": "[email protected]", "sender": "[email protected]" , "tags": [ "webhook-example"], "opens": [{ ""t : 1365111111} "클릭": [{ "ts : 1365111111,"url ":"http : // mandrill. com "}],"state " : "example", "_ id": "exampleaaaaaaaaaaaaaaaaaaaaaaaaa1", "_ version": "exampleaaaaaaaaaaaaaa"} "_ id": "exampleaaaaaaaaaaaaaaaaaaaaaaaaa1", "ts": 1422475458}] - "보낸", "메타 데이터" --- JSONDATA ---- Array
나는 $ json이 json 데이터를 출력하고 있지만, 구불 구불 한 브래킷으로 시작하는 것이 아니라 선행 및 끝 브래킷이 있음을 알게되었습니다. 그래서 데이터를 배열처럼 호출하기로 결정했지만 아무 소용이 없습니다.
테스트에서 대신 $ json = stripslashes (... 위의 json 데이터를 리터럴 문자열로 복사하여 붙여 넣었습니다. 선행/끝 대괄호를 제거하면 일부 데이터를 구문 분석 할 수있었습니다.
(stringify를 사용하여) 문자열로 변환 한 다음 앞/뒤에있는 대괄호를 제거/바꾸고 json을 구문 분석 할 수 있습니까? 과거 프로젝트에서 비슷한 문제가 있었고 내 값 내에서 특수 문자를 이스케이프하거나 형식을 변경하고 json을 구문 분석해야했습니다. 행운을 빈다. – tamak
대괄호에서 대괄호까지 JSON이 유효합니다. http://jsonlint.com'STRIPSLASHES :'은 유효한 JSON이 아니며 따옴표가 없습니다. –
'echo $ jsondata [0] [ 'msg'] [ 'sender']; ' – PhearOfRayne