Skip to content
Advertisement

PHP – Proper JSON format not being returned?

I’m using the below code to return JSON data to my mobile application (I’m posting data to the Stripe server to create a customer and return a key to my iOS application). That said, when I post to this endpoint, the customer and key are created in the Stripe backend (awesome), but the following error comes back to my app – is my PHP formatted incorrectly? I’m under the impression that my code should be returning JSON… but my error is telling me it’s not.

Error message:

Error Domain=com.alamofire.error.serialization.response Code=-1016 “Request failed: unacceptable content-type: text/html”

PHP:

<?php

$customer = StripeCustomer::create(array(

));
    
     $key = StripeEphemeralKey::create(
      ["customer" => $customer->id],
      ["stripe_version" => $_POST['api_version']]
    );

  header('Content-Type: application/json');
    exit(json_encode($key));
    
    
}

JSON response when navigating to PHP file in browser:

{"id":"ephkey_000","object":"ephemeral_key","associated_objects":[{"id":"cus_iD","type":"customer"}],"created":1601166469,"expires":1601170069,"livemode":false,"secret":"ek_test_key_here"}

Advertisement

Answer

You can test your script in the browser by adding $_POST = ['api_version' => 'whatever' ]; just to see what JSON the script outputs. I’m guessing something else in the script is throwing an error, causing PHP to output a Content-Type: text/html header.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement