php curl多次请求返回慢与curl自身域名DNS转换有关,修改成IP请求正常

php-curl请求接口一般都会碰到的问题,记录一下;

请求域名对应host添加到请求头中

<?php

$host = gethostbyname('kingcard.dgunicom.com');

curl_setopt($ch, CRULOPT_HTTPHEADER, ['Host: '.$host]);

?>

$ch = curl_init();
// curl_setopt($ch, CURLOPT_URL, 'https://kingcard.dgunicom.com/dgZop/api/numSelect');
curl_setopt($ch, CURLOPT_URL, 'https://211.95.193.53/dgZop/api/numSelect');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);

$headers = array();
$headers[] = 'Host: kingcard.dgunicom.com';
$headers[] = 'Content-Type: application/json';
$headers[] = 'Expect:';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);
echo $result;

 来源: https://www.ligengxin.com/index/Article/info.html?cate=5&id=102

posted @ 2021-12-03 15:30  1553  阅读(460)  评论(0编辑  收藏  举报