{"id":1103,"date":"2014-11-05T11:35:59","date_gmt":"2014-11-05T03:35:59","guid":{"rendered":"http:\/\/www.awaysoft.com\/taor\/?p=1103"},"modified":"2014-11-05T12:12:22","modified_gmt":"2014-11-05T04:12:22","slug":"php-curl-get%e7%bd%91%e9%a1%b5%e5%86%85%e5%ae%b9","status":"publish","type":"post","link":"https:\/\/actom.me\/blog\/php-curl-get%e7%bd%91%e9%a1%b5%e5%86%85%e5%ae%b9.html","title":{"rendered":"php curl get\u7f51\u9875\u5185\u5bb9"},"content":{"rendered":"<pre class=\"lang:php decode:true\">function get_url_contents($url)\r\n{\r\n    $ch = curl_init();\r\n    $timeout = 5;  \r\n    curl_setopt ($ch, CURLOPT_URL,$url);\r\n    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); \r\n    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);\r\n    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  \/\/ \u9047\u5230302\u81ea\u52a8\u8df3\u8f6c\r\n    $file_contents = curl_exec($ch);\r\n    curl_close($ch);\r\n    return $file_contents;\r\n}<\/pre>\n<p>\u9047\u5230curl_setopt(): CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in \u9519\u8bef\uff0c\u53ef\u4ee5\u8fd8\u7528\u4e0b\u9762\u7684\u51fd\u6570\uff1a<\/p>\n<pre class=\"lang:php decode:true \">function get_url($url)\/\/\u83b7\u5f97url\u5730\u5740\u7684\u7f51\u9875\u5185\u5bb9\r\n{\r\n    $ch = curl_init();\r\n    $timeout = 5;  \r\n    curl_setopt ($ch, CURLOPT_URL,$url);\r\n    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); \r\n    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);\r\n    $file_contents = curl_redir_exec($ch); \/\/ \u7528curl_redir_exec\u66ff\u4ee3curl_exec\r\n    curl_close($ch);\r\n    return $file_contents;\r\n}\r\n\r\n\/* \u83b7\u53d6\u5185\u5bb9\u7684\u957f\u5ea6 *\/\r\nfunction get_content_length($str)\r\n{\r\n    $matches = array();\r\n    preg_match('\/Content-Length:(.*?)\\n\/', $str, $matches);\r\n    $len = @trim(array_pop($matches));\r\n    if (!$len) {\r\n        $len = 0;\r\n    }\r\n    return (int)$len;\r\n}\r\n\r\nfunction curl_redir_exec($ch)\r\n{\r\n    static $curl_loops = 0;\r\n    static $curl_max_loops = 20;  \/\/ \u6700\u5927\u5faa\u73af\u6b21\u6570\r\n\r\n    if ($curl_loops++ &gt;= $curl_max_loops)\r\n    {\r\n        $curl_loops = 0;\r\n        return FALSE;\r\n    }\r\n    curl_setopt($ch, CURLOPT_HEADER, true);\r\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\r\n    $data = curl_exec($ch);\r\n\/* \u5206\u79bbheader\u548ccontent *\/\r\n    $content_len = get_content_length($data);\r\n    $header = substr($data, 0, strlen($data) - $content_len);\r\n    $data = substr($data, strlen($header));\r\n\r\n    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);\r\n    if ($http_code == 301 || $http_code == 302) {\r\n        $matches = array();\r\n        preg_match('\/Location:(.*?)\\n\/', $header, $matches);\r\n        $url = @parse_url(trim(array_pop($matches)));\r\n        if (!$url)\r\n        {\r\n            $curl_loops = 0;\r\n            return $data;\r\n        }\r\n        $last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));\r\n        $new_url = $url['scheme'] . ':\/\/' . $url['host'] . $url['path'] . ($url['query']?'?'.$url['query']:'');\r\n        curl_setopt($ch, CURLOPT_URL, $new_url);\r\n        return curl_redir_exec($ch);\r\n    } else {\r\n        $curl_loops=0;\r\n        return $data;\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>function get_url_contents($url) { $ch =  &#8230; <a class=\"more-link\" href=\"https:\/\/actom.me\/blog\/php-curl-get%e7%bd%91%e9%a1%b5%e5%86%85%e5%ae%b9.html\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/posts\/1103"}],"collection":[{"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/comments?post=1103"}],"version-history":[{"count":3,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/posts\/1103\/revisions"}],"predecessor-version":[{"id":1106,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/posts\/1103\/revisions\/1106"}],"wp:attachment":[{"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/media?parent=1103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/categories?post=1103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/tags?post=1103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}