admin管理员组文章数量:1122832
I have the following code in one of my Wordpress page templates:
$url = "https://xxxxxxxxxxxxxxx/";
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
);
$args = [
"ctype" => sanitize_text_field($_GET['ctype']),
];
if ($args['ctype'] == "test") {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, wp_json_encode($args));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($ch), true);
// Check HTTP status code
if (!curl_errno($ch)) {
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "HTTP Code: $http_code";
} else {
echo 'cURL Error: ' . curl_error($ch) . "\n";
}
curl_close($ch);
}
My code is executing extremely slow. I initially had CURLOPT_TIMEOUT set to 10. This was waiting for 10 seconds for the script to execute. Then when I switch it to 2, it was running quite fast but I think there is still a bigger problem.
In the if (!curl_errno($ch))
statement, it always goes to the else
statement and echoes out cURL Error: Operation timed out after 2001 milliseconds with 0 bytes received. I don't really understand why it is always going into the else
statement.
I have tried other cURL directives such as:
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_setopt($ch, CURLOPT_SSL_SESSIONID_CACHE, true);
curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 3600);
curl_setopt($ch, CURLOPT_TCP_FASTOPEN, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
but none of these seemed to have any effect.
Detailed timings based on curl_setopt($ch, CURLOPT_TIMEOUT, 10)
:
CURLINFO_TOTAL_TIME: 10s
CURLINFO_NAMELOOKUP_TIME: 0.001s
CURLINFO_CONNECT_TIME: 0.156s
CURLINFO_PRETRANSFER_TIME: 0.473s
CURLINFO_STARTTRANSFER_TIME: 0s
CURLINFO_HEADER_OUT: nothing returned
I have the following code in one of my Wordpress page templates:
$url = "https://xxxxxxxxxxxxxxx/";
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
);
$args = [
"ctype" => sanitize_text_field($_GET['ctype']),
];
if ($args['ctype'] == "test") {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, wp_json_encode($args));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($ch), true);
// Check HTTP status code
if (!curl_errno($ch)) {
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "HTTP Code: $http_code";
} else {
echo 'cURL Error: ' . curl_error($ch) . "\n";
}
curl_close($ch);
}
My code is executing extremely slow. I initially had CURLOPT_TIMEOUT set to 10. This was waiting for 10 seconds for the script to execute. Then when I switch it to 2, it was running quite fast but I think there is still a bigger problem.
In the if (!curl_errno($ch))
statement, it always goes to the else
statement and echoes out cURL Error: Operation timed out after 2001 milliseconds with 0 bytes received. I don't really understand why it is always going into the else
statement.
I have tried other cURL directives such as:
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_setopt($ch, CURLOPT_SSL_SESSIONID_CACHE, true);
curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 3600);
curl_setopt($ch, CURLOPT_TCP_FASTOPEN, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
but none of these seemed to have any effect.
Detailed timings based on curl_setopt($ch, CURLOPT_TIMEOUT, 10)
:
CURLINFO_TOTAL_TIME: 10s
CURLINFO_NAMELOOKUP_TIME: 0.001s
CURLINFO_CONNECT_TIME: 0.156s
CURLINFO_PRETRANSFER_TIME: 0.473s
CURLINFO_STARTTRANSFER_TIME: 0s
CURLINFO_HEADER_OUT: nothing returned
Share
Improve this question
edited Jun 5, 2024 at 11:57
user1448031
asked Jun 5, 2024 at 10:27
user1448031user1448031
1631 gold badge4 silver badges9 bronze badges
7
|
Show 2 more comments
1 Answer
Reset to default 0The timeout issue, in my case, was caused by the remote server not sending back any response. I had to fix configuration on the remote server to send back a response. That has resolved the issue.
本文标签: phpcURL running slow in Wordpress
版权声明:本文标题:php - cURL running slow in Wordpress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736304574a1932281.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wp_remote_post()
but it was giving a similar issue, so I resorted to PHP cURL. I've updated my post above to add some additional details about the timings. – user1448031 Commented Jun 5, 2024 at 11:57