jQuery / PHP to get_file_contents XML并返回json数组

时间:2016-03-06 12:12:05

标签: javascript php jquery json ajax

对PHP和AJAX没什么新意,我试图获取返回的XML并转换为JSON数组,这样我可以更好地显示数据,但似乎无法使其正常工作。有什么想法吗?

PHP

<?php

if (isset($_GET['reference'])){

    $xml = file_get_contents("http://www.tpeweb.co.uk/WebServices/Customer/PODTrackingData.asmx/GetTrackingRecord?AccountCode=".$_GET['account']."&Reference=".$_GET['reference']."");
    $json = json_decode(json_encode((array)$xml), TRUE);
    echo $json;

}
?>

HTML / JQUERY

<section class="inner rte">
    {{ page.content }}
    <div id="tracking"></div>
    <script type="text/javascript">
       var account = "01484267";
       var reference = "2838"; 
       $.ajax({
         url: 'http://www.ambientlounge.com/external/ukTracking.php',
         type: 'POST', 
         data: {
           account: account,
           reference: reference
         }, 
         success: function(xml){
           var obj = $.parseJSON(xml);
           $('#tracking').html(obj);
           console.log(obj);
         }
       });
    </script>
  </section>

我在PHP中遇到此错误:

  

PHP Parse错误:语法错误,意外&#39;,&#39;在第12行的/home/ambcom/public_html/external/ukTracking.php

$json = json_encode((array)simplexml_load_string($xml)),1);

2 个答案:

答案 0 :(得分:0)

您有解析错误。需要修改为:

class ZoomInCircleViewTransition: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {

    var transitionContext: UIViewControllerContextTransitioning?

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.6
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        self.transitionContext = transitionContext

        guard let toViewController: UIViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) else {
            return
        }

        guard let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) else {            return
        }

        guard let fromViewTransitionFromView = fromViewController as? TransitionFromViewProtocol else {
            return
        }

        let imageViewSnapshot = fromViewTransitionFromView.getViewForTransition()

        let endFrame = CGRectMake(-CGRectGetWidth(toViewController.view.frame)/2, -CGRectGetHeight(toViewController.view.frame)/2, CGRectGetWidth(toViewController.view.frame)*2, CGRectGetHeight(toViewController.view.frame)*2)

        if let containerView = transitionContext.containerView(){
            containerView.addSubview(fromViewController.view)
            containerView.addSubview(toViewController.view)
            containerView.addSubview(imageViewSnapshot)
        }


        let maskPath = UIBezierPath(ovalInRect: imageViewSnapshot.frame)
        let maskLayer = CAShapeLayer()
        maskLayer.frame = toViewController.view.frame
        maskLayer.path = maskPath.CGPath
        toViewController.view.layer.mask = maskLayer

        let quadraticEndFrame = CGRect(x: endFrame.origin.x - (endFrame.height - endFrame.width)/2, y: endFrame.origin.y, width: endFrame.height, height: endFrame.height)
        let bigCirclePath = UIBezierPath(ovalInRect: quadraticEndFrame)

        let pathAnimation = CABasicAnimation(keyPath: "path")
        pathAnimation.delegate = self
        pathAnimation.fromValue = maskPath.CGPath
        pathAnimation.toValue = bigCirclePath
        pathAnimation.duration = transitionDuration(transitionContext)
        maskLayer.path = bigCirclePath.CGPath
        maskLayer.addAnimation(pathAnimation, forKey: "pathAnimation")


        let hideImageViewAnimation =  {
            imageViewSnapshot.alpha = 0.0
        }

        UIView.animateWithDuration(0.2, delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: hideImageViewAnimation) { (completed) -> Void in
        }

        let scaleImageViewAnimation = {
            imageViewSnapshot.frame = quadraticEndFrame
        }
        UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: scaleImageViewAnimation) { (completed) -> Void in
            // After the complete animations have endet
            imageViewSnapshot.removeFromSuperview()
            toViewController.view.layer.mask = nil
        }
    }

    override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
        if let transitionContext = self.transitionContext {
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
        }
    }

    // MARK: UIViewControllerTransitioningDelegate protocol methods

    // return the animataor when presenting a viewcontroller
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }

    // return the animator used when dismissing from a viewcontroller
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }
}

$json = json_decode(json_encode($xml), TRUE); 此处数组意味着将您的数组放在此参数中json_encode(array)

答案 1 :(得分:0)

您所拥有的错误不在您共享的代码中,而是在文件json_encode($xml)中,问题在于您拥有不需要的/home/ambcom/public_html/external/ukTracking.php。改变这个

)

对于此

$json = json_encode((array)simplexml_load_string($xml)),1);