尝试使用php curl get请求访问api。要使get请求生效,我必须首先使用post请求进行身份验证,实际上是登录到平台。我从发布请求中得到了正确的答复,但我无法获得get请求。这是我的代码:
<?php
$login_url = "https://publisher-api.company.com/1.0/Publisher/Login";
$user_pswd = array(
"username" => "username1",
"password" => "password1"
);
$report_url = "https://publisher-api.company.com/1.0/Publisher(#####)/Channels/RevenueReport";
function httpGet($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_HEADER, true);
$output = curl_exec($ch);
if($output === false)
{
echo "Error Number:".curl_errno($ch)."<br>";
echo "Error String:".curl_error($ch);
}
echo $output;
}
function httpPost($url_post, $params, $url_get)
{
foreach($params as $key=>$value) { $params_string .=$key.'='.$value.'&'; }
rtrim($params_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_post);
curl_setopt($ch, CURLOPT_POST, count($params));
curl_setopt($ch, CURLOPT_POSTFIELDS, $params_string);
$result = curl_exec($ch);
echo $result;
httpGet($url_get);
curl_close($ch);
}
httpPost($login_url, $user_pswd, $report_url);
?>
这是回显的输出:
{"value":{"publisher":{"active":"1","publisher_id":"#####"}}}1{"error":{"code":"SYSTEM.SERVICE.NOT_AUTHORIZED","message":"You are not authorized to use this service. Authenticate first."}}
答案 0 :(得分:1)
在页面顶部设置error_reporting(E_ALL)
当您致电httpGet($urlG);
时,$urlG
在名为httpPost()
#1会阻止#2
我的眼睛在$uurl
,pparams
等网站流血......请让这些变量名更有意义