dtwncxs3547 2016-07-13 15:32
浏览 116

使用PHP的HTTP基本身份验证cURL

I am trying to pull in data from an API using HTTP Basic authentication. The HTTP requests to the API are protected with HTTP Basic authentication. HTTP Basic authentication consists of a token and secret.

I have tried many different techniques, but keep getting the response that authentication was not provided. I am not sure if the token:secret method is different from username:password but I cannot get this to authenticate.

stdClass Object ( [error_message] => Authentication not provided. )

Here is the API documentation - https://www.whatconverts.com/api/

<?php


$token = "xxx";
$secret = "yyy";
$response = get_web_page("https://leads.seekmomentum.com/api/v1/leads");
$resArr = array();
$resArr = json_decode($response);
echo "<pre>"; print_r($resArr); echo "</pre>";

function get_web_page($url) {
    $options = array(
        CURLOPT_RETURNTRANSFER => true,   // return web page
        CURLOPT_HEADER         => false,  // don't return headers
        CURLOPT_FOLLOWLOCATION => true,   // follow redirects
        CURLOPT_MAXREDIRS      => 10,     // stop after 10 redirects
        CURLOPT_ENCODING       => "",     // handle compressed
        CURLOPT_USERAGENT      => "test", // name of client
        CURLOPT_AUTOREFERER    => true,   // set referrer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,    // time-out on connect
        CURLOPT_TIMEOUT        => 120,    // time-out on response
        CURLOPT_HTTPAUTH       => "CURLAUTH_BASIC",  // authentication method
        CURLOPT_USERPWD        => "$token:$secret",  // authentication

    ); 


    $ch = curl_init($url);
    curl_setopt_array($ch, $options);

    $content  = curl_exec($ch);

    curl_close($ch);

    return $content;
}

?>
  • 写回答

2条回答 默认 最新

  • dqwd71332 2016-07-13 15:40
    关注

    This is wrong:

        CURLOPT_HTTPAUTH       => "CURLAUTH_BASIC",  // authentication method
                                  ^^^^^^^^^^^^^^^^
    

    That's a string, not a curl constant. Try

        CURLOPT_HTTPAUTH       => CURLAUTH_BASIC,  // authentication method
    

    instead.

    it's the difference between:

    define('FOO', 'bar');
    
    echo FOO // outputs bar
    echo "FOO" // outputs FOO
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM