PHP curl exec在php脚本同一个域上失败 [英] PHP curl exec fail on php script same domain

查看:158
本文介绍了PHP curl exec在php脚本同一个域上失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用php curl从同一个网域网址中的php指令码取得内容。但我得到curl_exec错误。卷曲错误代码为28或操作超时。经过几天的调试,我发现它工作在非脚本页面像htm,而不是php,它也适用于如果url是不同的域上的脚本。我已经调试了几天,发现没有解决方案。非常感谢。

  $ url ='http:// ...' 
$ agent ='';
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_CONNECTTIMEOUT,8);
curl_setopt($ ch,CURLOPT_TIMEOUT,8);
$ result = curl_exec($ ch);
print< pre> \\\
;
print_r(curl_getinfo($ ch));
//获取错误信息echo\\\
\\\
cURL错误号:.curl_errno($ ch);
//打印错误信息echo\\\
\\\
cURL error:。 curl_error($ ch);
print< / pre> \\\
;
curl_close($ ch);
echo $ result;

cURL错误号码:28 cURL错误:



< blockquote>

操作超时8000
毫秒,接收到0字节


c $ c> $ url = http://.../page.htm
失败: $ url = http://.../page。

解决方案

一些服务器实际上甚至不响应这些请求(因此客户端不知道连接断开)。



将以下代码添加到代码中:

  curl_setopt($ ch,CURLOPT_USERAGENT,$ _SERVER ['HTTP_USER_AGENT']); 

此外,我在CURL功能中使用以下内容:

  curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,true); 
curl_setopt($ ch,CURLOPT_MAXREDIRS,50);
if(substr($ url,0,8)=='https://'){
//以下确保SSL始终有效。一个细节:
// SSL同时做两件事:
// 1.它加密通信
// 2.它确保目标方是他声称的。
//总之,如果允许下面的代码,CURL不会检查
//证书是否已知和有效,但是它仍然加密通信。
curl_setopt($ ch,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false);
}


I use php curl to get content from a php script in the same domain url. But I get curl_exec error. The curl error code is 28 or operation timed out. After days of debugging, I found that it works on non script page like htm, but not php, it also works if the url is a script on different domain. I have been debugging for days and found no solutions. Helps appreciated.

$url = 'http://...';
$agent = '';
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 8);
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
$result = curl_exec ($ch);
print "<pre>\n";
print_r(curl_getinfo($ch));
// get error info echo "\n\ncURL error number:" .curl_errno($ch);
// print error info echo "\n\ncURL error:" . curl_error($ch);
print "</pre>\n";
curl_close ($ch);
echo $result;

cURL error number:28 cURL error:

Operation timed out after 8000 milliseconds with 0 bytes received

Okay: $url = http://.../page.htm Fail: $url = http://.../page.php

解决方案

You're not setting the User Agent. Some servers actually do not even respond to such requests (thus the client doesn't know the connection dropped).

Add the following to your code:

curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

Additionally, I use the following in my CURL functionality:

curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_MAXREDIRS,50);
if(substr($url,0,8)=='https://'){
    // The following ensures SSL always works. A little detail:
    // SSL does two things at once:
    //  1. it encrypts communication
    //  2. it ensures the target party is who it claims to be.
    // In short, if the following code is allowed, CURL won't check if the 
    // certificate is known and valid, however, it still encrypts communication.
    curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
}

这篇关于PHP curl exec在php脚本同一个域上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆