Search code examples
phpcurlmixpanel

Bash CURL to PHP CURL translation


I am having trouble translation the following query from a tutorial (Mixpanel JQL) written in bash, to a PHP Curl query:

Bash code

# sends the JQL code in `query.js` to the api
# to a project with sample data for this tutorial
curl https://mixpanel.com/api/2.0/jql \
    -u ce08d087255d5ceec741819a57174ce5: \
    --data-urlencode script@query.js | python -m json.tool

Questions

  • In PHP, which CURL options should i use to do the equivalent;
  • What would be the path to script (i'm guessing an accessible HTTP url?);
  • What does python -m json.tool actually do and do I need it ?

Reference: https://mixpanel.com/help/reference/jql/getting-started

Thanks you.


Solution

  • I assume you removed your password, as -u is HTTP auth. The following example has password where you need to place it. (remove the stars though!).

    python -m json.tool is what the curl command is being piped in to, it's a json formatter. So I assume your service is returning a json format.

    I'm not sure what your file script@query.js is, so I assumed it's a filename. And thus added file_get_contents to it.

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://mixpanel.com/api/2.0/jql");
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_USERPWD, "ce08d087255d5ceec741819a57174ce5:*password*");
    curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode(file_get_contents("script@query.js")));
    $result=curl_exec ($ch);
    curl_close ($ch);