Solved

Problem with integration of PHP API

  • 27 April 2021
  • 1 reply
  • 14 views

I try to add Mixpanel to our products to track details such as PHP version, installed product version or active settings. For this, I have added the following code to the relevant .php file:

// ...

require_once PROJECT_PATH . '/vendor/autoload.php';
$mp = Mixpanel::getInstance( $project_id, [ 'debug' => true ] );
$mp->track( 'stats', [ 'test'=> '123' ] );

// ...

However, the api.mixpanel.com server always returns the value 0 without any other explanations.


Here’s the debug output (from the log file):

[ Base_MixpanelBase - line 76 ] : Using token: 62
[ Base_MixpanelBase - line 175 ] : Using consumer: curl -> ConsumerStrategies_CurlConsumer
[ Base_MixpanelBase - line 18 ] : Instantiated new Consumer
[ Base_MixpanelBase - line 76 ] : Using token: 62
[ Base_MixpanelBase - line 175 ] : Using consumer: curl -> ConsumerStrategies_CurlConsumer
[ Base_MixpanelBase - line 18 ] : Instantiated new Consumer
[ Base_MixpanelBase - line 196 ] : Queued message: {"event":"stats","properties":{"mp_lib":"php","distinct_id":"606df816-4459-e0d3-8de0-4ae61d9594aa","test":"123","token":62,"time":1619542252}}
[ Base_MixpanelBase - line 94 ] : destruct flush attempt #1
[ Base_MixpanelBase - line 113 ] : Flush called - queue size: 1
[ Base_MixpanelBase - line 114 ] : Making blocking cURL call to https://api-eu.mixpanel.com/track
[ ConsumerStrategies_CurlConsumer - line 150 ] : 0
[ Base_MixpanelBase - line 123 ] : Batch consumption failed!
[ Base_MixpanelBase - line 128 ] : added batch back to queue, queue size is now 1
[ Base_MixpanelBase - line 135 ] : Batch of 1 consumed, queue size is now 1

// Last 7 lines repeat 10 times)

I tried to manually call the API endpoint to test the connection, but also got a 0 response.

 

What am I doing wrong?

icon

Best answer by PhilippStracker 28 April 2021, 10:21

After digging through the REST API docs I was able to locate the problem:

The mixpanel-php library uses base64_encode() to encode the JSON data:

https://github.com/mixpanel/mixpanel-php/blob/0eb1c099495b82a996846d7f6cc0c7d31fa77325/lib/ConsumerStrategies/AbstractConsumer.php#L29

After removing the base64_encode (but keeping the json_encode call) the API responds with the expected 1 result and the events show up in Reporting.

I’m going to ditch the mixpanel-php library and use a custom built API for this

View original

1 reply

After digging through the REST API docs I was able to locate the problem:

The mixpanel-php library uses base64_encode() to encode the JSON data:

https://github.com/mixpanel/mixpanel-php/blob/0eb1c099495b82a996846d7f6cc0c7d31fa77325/lib/ConsumerStrategies/AbstractConsumer.php#L29

After removing the base64_encode (but keeping the json_encode call) the API responds with the expected 1 result and the events show up in Reporting.

I’m going to ditch the mixpanel-php library and use a custom built API for this

Reply


Mixpanel