从PHP中获取Serialized数组中的元素

时间:2015-12-01 02:37:15

标签: php arrays serialization paypal

我正在通过Paypal Sandbox应用程序接受付款的最后阶段。 'Success','paymentID','Token'和'PayerID'都被罚款。但是,事务作为序列化数组传递。

当我尝试使用var_dump转储数组时,会显示以下内容:

    object(PayPal\Api\Payment)#7 (1) { ["_propMap":"PayPal\Common\PayPalModel":private]=> array(8) { ["id"]=> string(28) "PAY-2XY54251XB3181942KZOPKIA" ["intent"]=> string(4) "sale" ["state"]=> string(8) "approved" ["cart"]=> string(17) "15C96215RJ375133A" ["payer"]=> object(PayPal\Api\Payer)#14 (1) { ["_propMap":"PayPal\Common\PayPalModel":private]=> array(3) { ["payment_method"]=> string(6) "paypal" ["status"]=> string(8) "VERIFIED" ["payer_info"]=> object(PayPal\Api\PayerInfo)#9 (1) { ["_propMap":"PayPal\Common\PayPalModel":private]=> array(7) { ["email"]=> string(20) "test@xxxxxxxxxx" ["first_name"]=> string(4) "Mr" ["last_name"]=> string(7) "Test" ["payer_id"]=> string(13) "4CTHQUQHGE87R" ["shipping_address"]=> object(PayPal\Api\ShippingAddress)#11 (1) { ["_propMap":"PayPal\Common\PayPalModel":private]=> array(6) { ["recipient_name"]=> string(12) "Mr Test" ["line1"]=> string(14) "1 Main Terrace" ["city"]=> string(13) "Wolverhampton" ["state"]=> string(13) "West Midlands" ["postal_code"]=> string(7) "W12 4LQ" ["country_code"]=> string(2) "GB" } } ["phone"]=> string(10) "0358302031" ["country_code"]=> string(2) "GB" } } } } ["transactions"]=> array(1) { [0]=> object(PayPal\Api\Transaction)#13 (1) { ["_propMap":"PayPal\Common\PayPalModel":private]=> array(5) { ["amount"]=> object(PayPal\Api\Amount)#17 (1) { ["_propMap":"PayPal\Common\PayPalModel":private]=> array(3) { ["total"]=> string(4) "5.36" ["currency"]=> string(3) "GBP" ["details"]=> object(PayPal\Api\Details)#19 (1) { ["_propMap":"PayPal\Common\PayPalModel":private]=> array(1) { ["subtotal"]=> string(4) "5.36" } } } } ["description"]=> string(29) "Test Purchase" ["invoice_number"]=> string(24) "INV-PP-2015-12-01-011720" ["item_list"]=> object(PayPal\Api\ItemList)#21 (1) { ["_propMap":"PayPal\Common\PayPalModel":private]=> array(2) { ["items"]=> array(0) { } ["shipping_address"]=> object(PayPal\Api\ShippingAddress)#24 (1) { ["_propMap":"PayPal\Common\PayPalModel":private]=> array(6) { ["recipient_name"]=> string(12) "Mr Test" ["line1"]=> string(14) "1 Main Terrace" ["city"]=> string(13) "Wolverhampton" ["state"]=> string(13) "West Midlands" ["postal_code"]=> string(7) "W12 4LQ" ["country_code"]=> string(2) "GB" } } } } ["related_resources"]=> array(1) { [0]=> object(PayPal\Api\RelatedResources)#26 (1) { ["_propMap":"PayPal\Common\PayPalModel":private]=> array(1) { ["sale"]=> object(PayPal\Api\Sale)#28 (1) { ["_propMap":"PayPal\Common\PayPalModel":private]=> array(11) { ["id"]=> string(17) "49B132929Y668630A" ["state"]=> string(9) "completed" ["amount"]=> object(PayPal\Api\Amount)#30 (1) { ["_propMap":"PayPal\Common\PayPalModel":private]=> array(3) { ["total"]=> string(4) "5.36" ["currency"]=> string(3) "GBP" ["details"]=> object(PayPal\Api\Details)#31 (1) { ["_propMap":"PayPal\Common\PayPalModel":private]=> array(1) { ["subtotal"]=> string(4) "5.36" } } } } ["payment_mode"]=> string(16) "INSTANT_TRANSFER" ["protection_eligibility"]=> string(8) "ELIGIBLE" ["protection_eligibility_type"]=> string(56) "ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE" ["transaction_fee"]=> object(PayPal\Api\Currency)#33 (1) { ["_propMap":"PayPal\Common\PayPalModel":private]=> array(2) { ["value"]=> string(4) "0.38" ["currency"]=> string(3) "GBP" } } ["parent_payment"]=> string(28) "PAY-2XY54251XB3181942KZOPKIA" ["create_time"]=> string(20) "2015-12-01T01:20:56Z" ["update_time"]=> string(20) "2015-12-01T01:20:56Z" ["links"]=> array(3) { [0]=> object(PayPal\Api\Links)#35 (1) { ["_propMap":"PayPal\Common\PayPalModel":private]=> array(3) { ["href"]=> string(65) "https://api.sandbox.paypal.com/v1/payments/sale/49B132929Y668630A" ["rel"]=> string(4) "self" ["method"]=> string(3) "GET" } } [1]=> object(PayPal\Api\Links)#36 (1) { ["_propMap":"PayPal\Common\PayPalModel":private]=> array(3) { ["href"]=> string(72) "https://api.sandbox.paypal.com/v1/payments/sale/49B132929Y668630A/refund" ["rel"]=> string(6) "refund" ["method"]=> string(4) "POST" } } [2]=> object(PayPal\Api\Links)#37 (1) { ["_propMap":"PayPal\Common\PayPalModel":private]=> array(3) { ["href"]=> string(79) "https://api.sandbox.paypal.com/v1/payments/payment/PAY-2XY54251XB3181942KZOPKIA" ["rel"]=> string(14) "parent_payment" ["method"]=> string(3) "GET" } } } } } } } } } } } ["create_time"]=> string(20) "2015-12-01T01:20:56Z" ["links"]=> array(1) { [0]=> object(PayPal\Api\Links)#39 (1) { ["_propMap":"PayPal\Common\PayPalModel":private]=> array(3) { ["href"]=> string(79) "https://api.sandbox.paypal.com/v1/payments/payment/PAY-2XY54251XB3181942KZOPKIA" ["rel"]=> string(4) "self" ["method"]=> string(3) "GET" } } } } } 

我试图从字符串中获取值'invoice_number','id'和'amount'无效,因此我可以使用这些值,并在批准购买时充值人员帐户。

这是我的代码,你可以解释一下我出错的地方因为我迷路了吗?

<?php

use PayPal\Api\Payment;
use PayPal\Api\PaymentExecution;

require 'paypalapp/start.php';

if (!isset($_GET['success'], $_GET['paymentId'], $_GET['PayerID'])) {
header("Location: http://www.domain.co.uk/fail.php");

}

if ((bool)$_GET['success'] === false) {
header("Location: http://www.domain.co.uk/fail.php");
}

$paymentId = $_GET['paymentId'];
$payerId = $_GET['PayerID'];
$payment = Payment::get($paymentId, $paypal);

$execute = new PaymentExecution();
$execute->setPayerId($payerId);

try {
    $result = $payment->execute($execute, $paypal);
} catch (Exception $e) {
    $data = json_decode($e->getData());
    echo $data->message;
}

var_dump($payment);

echo 'Payment Made.  Thanks!';

?>

干杯!

1 个答案:

答案 0 :(得分:1)

如评论中所述,您只需要unserialize()响应对象:

class A
{
 public:
  A(){};
  virtual ~A(){};

  virtual float func(const int in1, const int in2, const int in3, int 4 = some_def_value) = 0;

  //
  // Some default useful functions
  //
 private:
  //
  // Some infrastructure code
  //

注意: 当返回一个对象时,您需要将其作为对象($getval = unserialize($payment); echo $getval->id; )而不是数组进行访问(->)。