Bash CURL到PHP CURL的翻译


Bash CURL to PHP CURL translation

我很难将以下查询从用bash编写的教程(MixtpanelJQL)转换为PHP Curl查询:

Bash代码

# 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

问题

  • 在PHP中,我应该使用哪些CURL选项来进行等效操作
  • 脚本的路径是什么(我猜是一个可访问的HTTP url?)
  • python -m json.tool实际做什么?我需要它吗

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

谢谢你。

我假设您删除了密码,因为-u是HTTP身份验证。下面的例子有密码在你需要放置它的地方。(不过要删除星号!)。

python-mjsontool是curl命令的管道接口,它是一个json格式化程序。所以我假设您的服务返回的是json格式。

我不确定你的档案是什么script@query.js是,所以我假设它是一个文件名。因此添加了file_get_contents。

$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);

以下是工作原理,对ba的调用实际上很简单。

呼叫

请求URL(GET)https://mixpanel.com/api/2.0/jql?script=<javscript script contents>

在PHP中

$scriptContents = file_get_contents(<FILE PATH>);
$request_url = 'https://mixpanel.com/api/2.0/jql?script='.urlencode($scriptContents);
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => <Request URL>,
    CURLOPT_CONNECTTIMEOUT => 2,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_HTTPAUTH => 1,
    CURLAUTH_ANY => 1,
    CURLOPT_USERPWD => 'ce08d087255d5ceec741819a57174ce5',
));
$data = curl_exec($curl);
curl_close($curl);