我正在尝试从URL获取JSON对象。我尝试过使用PHP(file_get_contents
和curl)和Python(都使用urllib2
和requests
)。
所有错误,但如果我使用浏览器访问URL,则会显示JSON对象。这是我在Apache2上的应用程序。
以下是我获得的每个命令的错误:
Python请求 -
File "test.py", line 4, in <module>
response= requests.get(url, timeout=2)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 55, in get
return request('get', url, * *kwargs)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, *kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 455, in request
resp = self.send(prep, * *send_kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 558, in send
r = adapter.send(request, * *kwargs)
File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 387, in send
raise Timeout(e)
requests.exceptions.Timeout: (<urllib3.connectionpool.VerifiedHTTPSConnection instance at 0x7fbb3ec6d440>, 'Connection to **url** timed out. (connect timeout=2)')
我也尝试使用php在线模拟器运行file_get_contents只是因为它与我的服务器有关但是我收到了这个错误
E_WARNING : type 2 -- file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed -- at line 4
E_WARNING : type 2 -- file_get_contents(): Failed to enable crypto -- at line 4
卷曲代码是:
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
if ( $result==false ) {
echo curl_errno($ch).' '.curl_error($ch);
}
curl_close($ch);