0

Problem

On production server, my code (See below) produces an unexpected 401 error. However, it works fine on my development server. I have been trying to fix performance caused by the unexpected 401 error. Since my code works fine on Development server, I don't believe the PHP code is causing the error.

Any advise or guidance would be greatly appreciated!


Code

$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); 
curl_setopt($ch, CURLOPT_USERPWD, $VAR:$VAR2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "|MY API V1.0|");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json','Content-Length: ' . strlen($POST)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POST);

These are the curl_info responses for both, production and development, servers.

Production (Slow Performance)

CURL Info

Array ( 
  [url] => https://example.com/v0.1.0/token/create-token.hcw
  [content_type] => text/html; charset=utf-8 
  [http_code] => 200
  [header_size] => 685 
  [request_size] => 766 
  [filetime] => -1
  [ssl_verify_result] => 18 
  [redirect_count] => 1 
  [total_time] => 20.13144 
  [namelookup_time] => 0.000381 
  [connect_time] => 0.000473 
  [pretransfer_time] => 0.010784 
  [size_upload] => 0 
  [size_download] => 80 
  [speed_download] => 3 
  [speed_upload] => 0 
  [download_content_length] => 80 
  [upload_content_length] => 0 
  [starttransfer_time] => 0.067625 
  [redirect_time] => 20.063809 
) 

First Response (401)

HTTP/1.0 401 Unauthorized 
Date: Mon, 04 May 2015 02:19:46 GMT 
Server: Apache 
WWW-Authenticate: Digest realm="MY API",qop="auth",nonce="5546d75645e7d",opaque="5c4c3e3231714690a63d174
a9cf26780" 
Cache-Control: max-age=0 
Expires: Mon, 04 May 2015 02:19:46
GMT X-Powered-By: PleskLin 
X-UA-Compatible: IE=Edge,chrome=1
Content-Length: 111 
Connection: close 
Content-Type: text/html; charset=utf-8

Second Response (200 - After 20+ Seconds Delay)

HTTP/1.1 200 OK 
Date: Mon, 04 May 2015 02:20:06 GMT
Server: Apache 
Cache-Control: max-age=0 
Expires: Mon, 04 May 2015 02:20:06 GMT 
X-Powered-By: PleskLin 
X-UA-Compatible: IE=Edge,chrome=1
Content-Length: 80 
Connection: close 
Content-Type: text/html; charset=utf-8

JSON String:

{"response":"success", "content":[{"token":"ko67atw1CFRSEdllOyeklRIMPb1ZS0a0"}]}

Development (Good Performance)

CURL Info

Array ( 
  [url] => http://localhost/mydomain/v0.1.0/token/create-token.hcw
  [content_type] => text/html; charset=utf-8 
  [http_code] => 200 
  [header_size] => 736 
  [request_size] => 348 
  [filetime] => -1 
  [ssl_verify_result] => 0
  [redirect_count] => 1 
  [total_time] => 0.005946 
  [namelookup_time] => 8.0E-5 
  [connect_time] => 8.3E-5 
  [pretransfer_time] => 0.000133 
  [size_upload] => 20 
  [size_download] => 0 
  [speed_download] => 0
  [speed_upload] => 3363 
  [download_content_length] => 0
  [upload_content_length] => 20 
  [starttransfer_time] => 0.002765
  [redirect_time] => 0.003146
) 

First Response (200)

HTTP/1.1 200 OK 
Date: Mon, 04 May 2015 02:33:40 GMT 
Server: Apache/2.2.14 (Unix) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l PHP/5.3.1 mod_perl/2.0.4 Perl/v5.10.1 
X-Powered-By: PHP/5.3.1 
Cache-Control: max-age=0 
Expires: Mon, 04 May 2015 02:33:40 GMT 
X-UA-Compatible: IE=Edge,chrome=1 
Vary: Accept-Encoding
Content-Length: 0 
Content-Type: text/html; charset=utf-8 

Second Response (200)

HTTP/1.1 200 OK 
Date: Mon, 04 May 2015 02:33:40 GMT 
Server: Apache/2.2.14 (Unix) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l PHP/5.3.1 mod_perl/2.0.4 Perl/v5.10.1 
X-Powered-By: PHP/5.3.1 
Cache-Control: max-age=0 
Expires: Mon, 04 May 2015 02:33:40 GMT 
X-UA-Compatible: IE=Edge,chrome=1 
Vary: Accept-Encoding 
Content-Length: 0 
Content-Type: text/html; charset=utf-8

JSON String:

{"response":"success", "content":[{"token":"ko67atw1CFRSEdllOyeklRIMPb1ZS0a0"}]}
Leandro Papasidero
  • 3,728
  • 1
  • 18
  • 33

1 Answers1

0
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));

was the culprit. After many days it is now fixed.