我正在努力实现的目标:
获取对API端点的请求,检索XML并随后解析结果
我正在发送一个file_get_contents
请求来实现这一点。
问题:
`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.
更新17/08
巩固我目前的理解:
1.PHP失败:
1.a它通过php失败(超时)
1.b通过命令行失败(curl-Ghttp://api.eve-central.com/api/quicklook?typeid=34)
1.c文件集内容
1.d file_get_contents w/create_stream_text
2.工作原理:
2.a将url粘贴到chrome选项卡中
2.b通过邮递员
尝试的内容:-检查Postman中的Headers,并尝试通过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工作
- 如何修改我的代码,使其模仿Postman的操作
以前的尝试我尝试过的:来自其他线程的各种cURL选项,如
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打开
-allow_url_include=启用
-启用了相关的ssl扩展
-提高了超时窗口
-两者都通过php.ini
-也可以通过php文件中的显式声明
-尝试使用其他url
-相同的错误,所以它并不真正取决于我的特定端点
-例如,twitter/wikipedia/google都返回特定的错误-尝试使用:
-本地xml文件上的file_get_contents(https://msdn.microsoft.com/en-us/library/ms762271(v=vs.85).aspx)-->有效
-远程xml文件上的file_get_contents(http://www.xmlfiles.com/examples/note.xml)-->失败相同错误
-到目前为止,总体情况如下:
-卷曲失败,超时
-file_get_Contents失败,超时
-在浏览器中打开XML文件url有效
-通过Postman发出GET请求,使用
显然,在file_get_contents
通过php失败的所有情况下,我都可以通过任何浏览器轻松访问该文件。
试图解决这个问题
尝试1:
使用nitrous.io,创建LAMP堆栈,通过平台执行操作结果:fileget_contents是有效的,但是,由于要检索大量的xml文件,操作超时。暂定解决方案:
-从源下载XML文件
-压缩它们
-下载xml_file
-本地解析所述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;
}