douji6667 2015-08-08 10:31
浏览 174
已采纳

PHP CURL GET / POST摘要式身份验证

I am using curl method to fetch data from REST API. API requires digest authenticate. I have done digest authenticate but not working for post method. Working fine for GET method.

$username = 'username';
$password = 'password';

$method = 'GET';
// $method = 'POST';

//  FOR POST METHOD. API REQUIRE THIS FORMAT
// $fields = array('APIRquestData' => '{"name":"value","name1":["v4","v5"]}');

$url = "http://apiurl/getmethodname";
// $url = "http://apiurl/postmethodname";

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch,CURLOPT_TIMEOUT, 30);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 30);
if($method == 'POST')
{
  $fieldsData = http_build_query($fields);
  curl_setopt($ch,CURLOPT_POSTFIELDS, $fieldsData);
}

curl_setopt($ch, CURLOPT_HEADER, 1)
$first_response = curl_exec($ch);
$info = curl_getinfo($ch);

preg_match('/WWW-Authenticate: Digest (.*)/', $first_response, $matches);

if(!empty($matches))
{
  $auth_header = $matches[1];
  $auth_header_array = explode(',', $auth_header);
  $parsed = array();

  foreach ($auth_header_array as $pair)
  {
    $vals = explode('=', $pair);
    $parsed[trim($vals[0])] = trim($vals[1], '" ');
  }

  $response_realm     = (isset($parsed['realm'])) ? $parsed['realm'] : "";
  $response_nonce     = (isset($parsed['nonce'])) ? $parsed['nonce'] : "";
  $response_opaque    = (isset($parsed['opaque'])) ? $parsed['opaque'] : "";

  $authenticate1 = md5($username.":".$response_realm.":".$password);
  $authenticate2 = md5($method.":".$url);

  $authenticate_response = md5($authenticate1.":".$response_nonce.":".$authenticate2);

  $request = sprintf('Authorization: Digest username="%s", realm="%s", nonce="%s", opaque="%s", uri="%s", response="%s"',
  $username, $response_realm, $response_nonce, $response_opaque, $url, $authenticate_response);

  $request_header = array($request);

  $ch = curl_init();
  curl_setopt($ch,CURLOPT_URL, $url);
  curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch,CURLOPT_FOLLOWLOCATION, false);
  curl_setopt($ch,CURLOPT_TIMEOUT, 30);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 30);

  if($method == 'POST')
  {
    $fieldsData = http_build_query($fields);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fieldsData);
  }
  curl_setopt($ch, CURLOPT_HTTPHEADER, $request_header);

  $result['response']         = curl_exec($ch);
  $result['info']             = curl_getinfo ($ch);
  $result['info']['errno']    = curl_errno($ch);
  $result['info']['errmsg']   = curl_error($ch); 
}


/*
I am getting this as response

Array
(
  [response] =>  

  HTTP Status 404 - 
  type Status report
  message 
  description The requested resource () is not available.
  [info] => Array
    (
        [url] => http://apiurl/postmethodname
        [content_type] => text/html;charset=ISO-8859-1
        [http_code] => 404
        [header_size] => 361
        [request_size] => 551
        [filetime] => -1
        [ssl_verify_result] => 0
        [redirect_count] => 0
        [total_time] => 0.109
        [namelookup_time] => 0
        [connect_time] => 0.063
        [pretransfer_time] => 0.063
        [size_upload] => 114
        [size_download] => 956
        [speed_download] => 8770
        [speed_upload] => 1045
        [download_content_length] => 956
        [upload_content_length] => 114
        [starttransfer_time] => 0.109
        [redirect_time] => 0
        [redirect_url] => 
        [primary_ip] => XXX.X.XX.XX
        [certinfo] => Array
            (
            )

        [primary_port] => 80
        [local_ip] => XX.XX.XX.XXX
        [local_port] => 58850
        [errno] => 0
        [errmsg] => 
    )
)
*/

Response says that, 404 means URL not found. But URL is correct.

cURL Information 7.40.0

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dtrphb5597 2015-08-11 09:14
    关注

    Instead of hitting first curl use get_headers($url).

    add Content-Type:application/json in header.

    add $request_header[] = 'Content-Type:application/json'; this line after $request_header = array($request).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大