Bash CURL到PHP CURL转换

时间:2016-05-10 08:53:22

标签: php curl mixpanel

我无法将以下查询从用bash编写的教程(Mixpanel JQL)转换为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网址?);
  • python -m json.tool实际上做了什么,我需要做什么?

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

谢谢你。

2 个答案:

答案 0 :(得分:1)

我假设您删除了密码,因为-u是HTTP身份验证。以下示例包含密码,您需要将其放置在其中。 (虽然删除了星星!)。

python -m json.tool是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);

答案 1 :(得分:0)

这是有效的,对ba的调用很简单。

拨打

请求网址(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);