简体   繁体   English

Bash CURL到PHP CURL的翻译

[英]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编写的教程(Mixpanel JQL)转换为PHP Curl查询:

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; 在PHP中,我应该使用哪个CURL选项来做等效的工作;
  • What would be the path to script (i'm guessing an accessible HTTP url?); 脚本的路径是什么(我猜是可访问的HTTP URL?);
  • What does python -m json.tool actually do and do I need it ? python -m json.tool实际上是做什么的,我需要吗?

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

Thanks you. 谢谢。

I assume you removed your password, as -u is HTTP auth. 我假设您已删除密码,因为-u是HTTP身份验证。 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. python -m json.tool是curl命令的管道,它是json格式化程序。 So I assume your service is returning a json format. 因此,我假设您的服务正在返回json格式。

I'm not sure what your file script@query.js is, so I assumed it's a filename. 我不确定您的文件script@query.js是什么,因此我认为它是文件名。 And thus added file_get_contents to it. 从而添加了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);

Here is what worked, the call to ba made is actuallty simple. 这是有效的方法,对ba的调用实际上很简单。

Call 呼叫

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

In PHP 在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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM