我正在使用api(使用curl和php),它应该根据日期返回一批csv记录。首先,您发出一个请求,返回批次中的记录数。第二个请求是实际检索记录。我的问题是当我发出第二个请求时,我没有得到第一个请求表明我要获得的记录数。无论选择的日期如何,我总是获得7167条记录(任何特定日期的记录数量将超过8000条)。我收到的内容长度是我要获得的标题。我没有得到任何错误。如果我把url请求放在浏览器的地址栏中,我就得到了我应该得到的所有记录。
该脚本在Linux平台上运行。事实上,我已经在另一台具有相同结果的Linux服务器上尝试过它。我尝试过更改执行时间和超时设置。我真的很困惑。提前感谢您的建议。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
set_time_limit(0); // no limit
$username = "*********";
$pwd = "*********";
$urlCount = "https://*********&fromDate=". $today . "&toDate=" . $tomorrow";
$TotalPages = 0;
$CurrentPage = 0;
$PageSize = 1;
$RecordCount = 0;
$TotalRecordsProcessed = 0;
$today = date( "Y-m-d" );
$tomorrow = date( 'Y-m-d', strtotime( "+1 days" ) );
echo "today " . $today . "<br>";
echo "tomorrow: " . $tomorrow . "<br>";
$urlCount = "https://*********&fromDate=". $today . "&toDate=" . $tomorrow;
$headers = array("Content-Type:text/plain", 'Connection: Keep-Alive');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urlCount );// Get the count of the records we asked for
curl_setopt($ch, CURLOPT_HEADER, true );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true );
curl_setopt($ch, CURLOPT_TIMEOUT, 6000);
curl_setopt($ch, CURLOPT_VERBOSE, true );
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $pwd );
$result = curl_exec( $ch ); // execute the curl function
$lines = explode( "\r\n", $result );
$idx = 0;
while( $lines[ $idx ] != null ) $idx++; // find the empty line
$RecordCount = $lines[ $idx + 1 ]; // This is the index to the number of records in the set
$PageSize = $RecordCount;
echo "Records in set: " . $RecordCount . "<br>";
$url = "https://***********&fromDate=2015-10-19&toDate=2015-10-20&pageSize=" . $PageSize . "&pageNumber=" . $CurrentPage . "&timezone=Eastern&outputType=csv";
curl_setopt($ch, CURLOPT_URL, $url ); // set the url for getting the records
$result = curl_exec( $ch ); // execute the function
$info = curl_getinfo($ch);
curl_close( $ch );
$size_download = $info["size_download"];
echo "Http Code: " . $info['http_code'] . "<br>";
echo "Size download: " . $info["size_download"] . "<br>";
// Make sure the length of the content received is the same as the "size_download" from the http header
$pos = strpos( $result, "UUID" ) - 2; // find the begining of the content
$result = substr( $result , $pos, -1 ); // remove the header data...keep just the content
$len = strlen( $result ); // get the length of the received content
if( $len != $size_download )
{
echo "length:" . strlen( $result ) . "<br>";
echo "Content recieved not = size_download<br>";
}
// Check to make sure records downloaded match the number of records that the first curl request says there are
// Records are in csv format
$lines = explode( "\r\n", $result );
$RecordsReceived = count( $lines );
if( $RecordsReceived != $RecordCount )
{
echo "Record Count = " . $RecordCount . " RecordsReceived = " . $RecordsReceived . "<br>";
}
?>