文件获取内容通过php失败,通过浏览器工作
我正在努力实现的目标:文件获取内容通过php失败,通过浏览器工作,php,curl,timeout,file-get-contents,simplexml,Php,Curl,Timeout,File Get Contents,Simplexml,我正在努力实现的目标: 获取对API端点的请求,检索XML并随后解析结果。 我正在发送一个文件\u获取\u内容请求以实现此目的 问题: `file_get_Contents` fails, error: Warning: file_get_contents(https://api.twitter.com/1.1/statuses/mentions_timeline.json): failed to open stream: A connection attempt fai
获取对API端点的请求,检索XML并随后解析结果。
我正在发送一个
文件\u获取\u内容
请求以实现此目的
问题:
`file_get_Contents` fails, error:
Warning: file_get_contents(https://api.twitter.com/1.1/statuses/mentions_timeline.json):
failed to open stream:
A connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because
connected host has failed to respond.
2008年7月17日更新
巩固我目前的理解:1。PHP失败:
1.a通过php失败(超时)
1.b通过命令行(curl-G)失败
1.c文件\u获取\u内容
1.d文件\u获取\u内容w/创建\u流\u上下文 2。工作原理:
2.在chrome选项卡中粘贴url
2.b通过邮递员 尝试的内容: -检查Postman中的标题,并尝试通过php复制它们
Postman Headers sent back by eve-central:
Access-Control-Allow-Origin → *
Connection → Keep-Alive
Content-Encoding → gzip
Content-Type → text/xml; charset=UTF-8
Date → Wed, 17 Aug 2016 10:40:24 GMT
Proxy-Connection → Keep-Alive
Server → nginx
Transfer-Encoding → chunked
Vary → Accept-Encoding
Via → HTTP/1.1 proxy10014
对应代码:
$headers = array(
'method' => 'GET',
'header' => 'Connection: Keep-Alive',
'header' => 'Content-Encoding: gzip',
'header' => 'Content-Type: text/xml',
'header' => 'Proxy-Connection: Keep-Alive',
'header' => 'Server: nginx',
'header' => 'Transfer-Encoding: chunked',
'header' => 'Vary: Accept-Encoding',
'header' => 'Via: HTTP/1.1 proxy10014');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt($curl, CURLOPT_PORT , 8080); // Attempt at changing port in the event it was blocked.
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POST, false );
curl_setopt($curl, CURLOPT_URL, $url );
$resp = curl_exec($curl);
if(curl_error($curl))
{
echo 'error:' . curl_error($curl);
}
- 使用Wireshark捕获GET请求,查看更改端口是否有帮助
- 通过命令行运行cUrl
我没有主意和选择。 因此,问题是: - 如果它可以在浏览器和Postman中工作,为什么不能通过PHP工作
- 我如何修改我的代码,使其模仿邮递员的工作李>
function curl_get_contents($url) {
$ch = curl_init();
if (!$ch)
{
die("Couldn't initialize a cURL handle");
} else
echo "Curl Handle initialized ";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$data = curl_exec($ch);
// Check if any error occurred
if (!curl_errno($ch))
{
$info = curl_getinfo($ch);
echo 'Took ', $info['total_time'], ' seconds to send a request to ', $info['url'], "";
displayData($info);
} else
echo "Failed Curl, reason: ".curl_error($ch)." ";
curl_close($ch);
return $data;
}
结果:无,无数据返回。-选中php.ini选项:
-allow_fopen已打开
-允许\u url\u include=on
-相关的ssl扩展已启用
-提高了超时窗口
-两者都通过php.ini
-也可以通过php文件中的显式声明来实现。
-尝试使用不同的url
-同样的错误,因此它实际上并不取决于我的特定端点
-例如,twitter/wikipedia/google都返回特定的错误 -尝试使用:
-文件\u获取本地xml文件上的内容()-->有效
-文件获取远程xml文件内容()-->失败相同错误
-总的来说,到目前为止,以下是正确的:
-卷曲失败,超时
-文件获取内容失败,超时
-在浏览器中打开XML文件url有效
-通过邮递员发出GET请求,有效 显然,在所有情况下,
file\u get\u contents
通过php失败,我可以通过任何浏览器轻松访问该文件
试图解决这个问题。尝试1:
使用nitrous.io,创建灯堆,通过平台执行契约 结果:file_get_内容可以正常工作,但是,由于要检索大量xml文件,操作超时。 暂定解决方案:
-从源代码下载XML文件
-拉上拉链
-下载xml_文件
-本地解析所述xml文件
稍后,编写一个小的php脚本,当调用该脚本时,执行上面的位,将数据发送到本地目录,然后本地目录将其解压并对其执行其他工作。
另一种尝试是使用Google Sheets,通过用户函数将数据拉入工作表,然后将excel文件/值转储到mysql中。
就我而言,虽然这是一个非常无知的解决方案,但它确实起到了作用 用于避免共享主机超时问题的代码:
function downloadUrlToFile2($url, $outFileName)
{
//file_put_contents($xmlFileName, fopen($link, 'r'));
//copy($link, $xmlFileName); // download xml file
;
echo "Passing $url into $outFileName ";
// $outFileName = touch();
$fp = fopen($outFileName, "w");
if(is_file($url))
{
copy($url, $outFileName); // download xml file
} else
{
$ch = curl_init();
$options = array(
CURLOPT_TIMEOUT => 28800, // set this to 8 hours so we dont timeout on big files
CURLOPT_URL => $url
);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt_array($ch, $options);
$contents = curl_exec($ch);
fwrite($fp, $contents);
curl_close($ch);
}
}
我还在ini脚本的顶部添加了以下内容:
ignore_user_abort(true);
set_time_limit(0);
ini_set('memory_limit', '2048M');
我发现HTTPS url请求存在一些问题,对于修复问题,您必须在CURL请求中添加以下行
function curl_get_contents($url) {
$ch = curl_init();
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: ";
curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
// I have added below two lines
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
您正在尝试在不执行任何身份验证机制的情况下获取数据。为什么不试试Twitter的PHP包装器呢?谢谢你的回复。Twitter url只是用于尝试不同选项的随机url之一。如果执行文件获取内容($url),则结果不会更改,$url为:。因此,正如您从该url所看到的,它是一个普通的xml,不需要任何类型的身份验证,仍然会失败,并出现超时错误。代码在哪里运行?您是否检查过运行此操作的机器是否有直接的internet连接?(服务器可能位于preverse代理的后面)它可以解析名称吗?它没有防火墙阻止这种访问?它不受其他安全机制的限制?你查过日志了吗?出于安全原因(这是Redhat的SELinux策略的默认设置),配置Web服务器主机以防止它们通过internet进行传出连接是很常见的。我可以访问internet。例如,我可以通过Chrome访问,但不能通过文件获取内容或卷曲。然而,你的观点可能是正确的。我将尝试从我的家用电脑上执行相同的代码,以确保它不是代理/防火墙规则。尽管如此,如果我可以通过浏览器发出get请求,并正确显示xml,那么我应该也可以通过php这样做,不是吗?毕竟,它们使用相同的http堆栈(包括curl)来检索数据。多亏了这一点。我合并了您的反馈并添加了一些错误捕获:将代码添加到原始块。有趣的是,curl超时:从curl句柄获取内容初始化失败curl,原因:连接在5008之后超时milliseconds@user3375601我有一个URL也有同样的问题,它在浏览器中工作,但在php中通过文件\u get\u content和curl请求不工作。