为什么我可以使用浏览器而不是代码访问URL?

时间:2017-06-09 21:18:50

标签: php python json url

我正在尝试从URL获取JSON对象。我尝试过使用PHP(file_get_contents和curl)和Python(都使用urllib2requests)。 所有错误,但如果我使用浏览器访问URL,则会显示JSON对象。这是我在Apache2上的应用程序。

以下是我获得的每个命令的错误:

  • file_get_contents ---无法打开流
  • cURL ---错误28:连接在10001毫秒后超时
  • 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);

0 个答案:

没有答案