【问题标题】:Get the variable via CURL通过 CURL 获取变量
【发布时间】:2019-04-03 15:32:17
【问题描述】:

通过 API 指导,我正在尝试获取这些参数。但是我必须编写什么 php 代码才能将结果作为变量 $result 实际得到?

CURL 安装在服务器级别。对不起,我有点迷路了。谢谢!

curl -H "Content-Type:application/json" -X POST -d '{"start_date":"2016-08-02","end_date":"2016-08-03", "columns":["paid_impressions","revenue"]}' 'http://udmserve.com/udm/radalytics_api.cpx?action=report&api_key=xxxx'

【问题讨论】:

  • 堆栈溢出不是免费的代码转换服务。如果您下载“邮递员”之类的工具,那里有一个功能可以为您构建 PHP 代码。

标签: php html api curl


【解决方案1】:

此代码有效。不过,您需要对其进行修改以存储您的 api 密钥

<?php

//Initiate a curl request
$ch = curl_init();

//This is your API key
$apiKey = "xxxx";

//These are your postFields
$postFields = array(
        "start_date" => "2016-08-02",
        "end_date"   => "2016-08-03",
        "columns"    => array(
                "paid_impressions",
                "revenue"
            )
    );

//Setup the curl options using variables in place of your postFields and apiKey
curl_setopt($ch, CURLOPT_URL, "http://udmserve.com/udm/radalytics_api.cpx?action=report&api_key=" . $apiKey);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $postFields ));
curl_setopt($ch, CURLOPT_POST, 1);

//Set up the request headers
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

//Make the request
$result = curl_exec($ch);

//If there was an error, display error code
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

//Delete the curl object
curl_close ($ch);

//Show the result
echo $result;

//Or, print as array
echo '<pre>'. print_r( json_decode($result), 1 ) .'</pre>';

【讨论】:

猜你喜欢
  • 2017-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多