{"id":68488,"date":"2012-12-17T16:52:56","date_gmt":"2012-12-17T09:52:56","guid":{"rendered":"http:\/\/www.icez.net\/blog\/?p=68488"},"modified":"2012-12-17T16:52:56","modified_gmt":"2012-12-17T09:52:56","slug":"php-file_get_contents-replacement-for-curl","status":"publish","type":"post","link":"https:\/\/www.icez.net\/blog\/68488\/php-file_get_contents-replacement-for-curl","title":{"rendered":"0138 | function get url \u0e1c\u0e48\u0e32\u0e19 https \u0e43\u0e0a\u0e49\u0e41\u0e17\u0e19 curl"},"content":{"rendered":"<p>\u0e27\u0e31\u0e19\u0e01\u0e48\u0e2d\u0e19\u0e40\u0e08\u0e2d\u0e1a\u0e31\u0e4a\u0e01\u0e23\u0e30\u0e2b\u0e27\u0e48\u0e32\u0e07 curl + openssl + centos 6 \u0e19\u0e34\u0e14\u0e2b\u0e19\u0e48\u0e2d\u0e22<br \/>\n\u0e17\u0e35\u0e48\u0e17\u0e33\u0e43\u0e2b\u0e49 curl \u0e41\u0e25\u0e30 app \u0e17\u0e38\u0e01\u0e2d\u0e22\u0e48\u0e32\u0e07\u0e17\u0e35\u0e48\u0e43\u0e0a\u0e49 libcurl \u0e15\u0e48\u0e2d\u0e44\u0e1b\u0e2b\u0e32 server \u0e17\u0e35\u0e48\u0e40\u0e1b\u0e47\u0e19 https \u0e1a\u0e32\u0e07\u0e15\u0e31\u0e27\u0e1a\u0e19\u0e1a\u0e32\u0e07\u0e40\u0e07\u0e37\u0e48\u0e2d\u0e19\u0e44\u0e02\u0e44\u0e21\u0e48\u0e44\u0e14\u0e49<br \/>\n\u0e2b\u0e32\u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25\u0e44\u0e1b\u0e01\u0e47\u0e1e\u0e1a\u0e27\u0e48\u0e32\u0e15\u0e49\u0e2d\u0e07\u0e44\u0e1b\u0e41\u0e01\u0e49\u0e17\u0e35\u0e48\u0e1d\u0e31\u0e48\u0e07 server (\u0e08\u0e30\u0e1a\u0e49\u0e32\u0e40\u0e23\u0e2d\u0e30 server \u0e0a\u0e32\u0e27\u0e1a\u0e49\u0e32\u0e19) \u0e44\u0e21\u0e48\u0e01\u0e47\u0e15\u0e49\u0e2d\u0e07\u0e41\u0e01\u0e49 code \u0e43\u0e2b\u0e21\u0e48\u0e43\u0e2b\u0e49\u0e43\u0e0a\u0e49 library \u0e2d\u0e22\u0e48\u0e32\u0e07\u0e2d\u0e37\u0e48\u0e19\u0e41\u0e17\u0e19 \u0e01\u0e47\u0e40\u0e25\u0e22\u0e40\u0e01\u0e34\u0e14\u0e21\u0e32\u0e40\u0e1b\u0e47\u0e19 function \u0e19\u0e35\u0e49\u0e04\u0e23\u0e31\u0e1a<\/p>\n<p>\u0e21\u0e35 requirement \u0e19\u0e34\u0e14\u0e2b\u0e19\u0e48\u0e2d\u0e22\u0e04\u0e37\u0e2d PHP 5.3.4 \u0e02\u0e36\u0e49\u0e19\u0e44\u0e1b \u0e41\u0e25\u0e30 \u0e40\u0e1b\u0e34\u0e14 allow_url_fopen \u0e44\u0e27\u0e49\u0e43\u0e19 php.ini<\/p>\n<p>feature<br \/>\n=> custom header \u0e44\u0e14\u0e49<br \/>\n=> \u0e23\u0e2d\u0e07\u0e23\u0e31\u0e1a cookie \u0e42\u0e14\u0e22\u0e2d\u0e31\u0e15\u0e42\u0e19\u0e21\u0e31\u0e15\u0e34<br \/>\n=> method \u0e44\u0e14\u0e49\u0e17\u0e31\u0e49\u0e07 get \u0e41\u0e25\u0e30 post (\u0e23\u0e27\u0e21\u0e16\u0e36\u0e07 post \u0e41\u0e1a\u0e1a\u0e43\u0e2a\u0e48 data \u0e14\u0e49\u0e27\u0e22)<br \/>\n=> url \u0e44\u0e14\u0e49\u0e17\u0e31\u0e49\u0e07 http \u0e41\u0e25\u0e30 https (\u0e21\u0e35 validate url \u0e40\u0e1c\u0e37\u0e48\u0e2d\u0e44\u0e27\u0e49\u0e14\u0e49\u0e27\u0e22)<br \/>\n=> \u0e1a\u0e31\u0e07\u0e04\u0e31\u0e1a verify ssl certificate \u0e17\u0e35\u0e48\u0e1d\u0e31\u0e48\u0e07\u0e1b\u0e25\u0e32\u0e22\u0e17\u0e32\u0e07\u0e14\u0e49\u0e27\u0e22\u0e27\u0e48\u0e32 cert \u0e16\u0e39\u0e01\u0e15\u0e49\u0e2d\u0e07 (file get content \u0e1b\u0e01\u0e15\u0e34\u0e08\u0e30\u0e44\u0e21\u0e48 verify)<br \/>\n=> \u0e43\u0e0a\u0e49 ca file \u0e02\u0e2d\u0e07 OS (\u0e41\u0e01\u0e49\u0e44\u0e14\u0e49)<\/p>\n<pre lang=\"php\">&lt;?php\r\nfunction httpquery($url, $method = 'GET', $headers = array(), $body = '', $returnheader = false, $followlocation = true){\r\n    global $cookies;\r\n    $reqinfo = parse_url($url);\r\n    if (empty($reqinfo['host']) || empty($reqinfo['path'])) return false;\r\n    if (empty($reqinfo['scheme'])) $reqinfo['scheme'] = 'http';\r\n    if ($reqinfo['scheme'] != 'http' && $reqinfo['scheme'] != 'https') {\r\n        trigger_error(\"Unsupported scheme '\".$reqinfo['scheme'].\"'\");\r\n        return false;\r\n    }\r\n    if (empty($reqinfo['port'])) {\r\n        if ($reqinfo['scheme'] == 'https')\r\n            $reqinfo['port'] = 443;\r\n        else\r\n            $reqinfo['port'] = 80;\r\n    }\r\n\r\n    $headers['User-Agent'] = (!isset($headers['User-Agent']) ? 'Mozilla\/4.0 (compatible; icez.net http client)' : $headers['User-Agent']);\r\n    $headers['Host'] = $reqinfo['host'];\r\n    $headers['Accept'] = '*\/*';\r\n    \r\n    \r\n    $cdata = array();\r\n    foreach($cookies as $ckey => $cval) {\r\n        $cdata[] = $ckey.'='.$cval;\r\n    }\r\n    $headers['Cookie'] = implode('; ', $cdata); \/\/ http_build_query($cookies);\r\n    unset($cdata);\r\n\r\n    if ($method == 'POST') {\r\n        $headers['Content-Type'] = (isset($headers['Content-Type']) ? $headers['Content-Type'] : 'application\/x-www-form-urlencoded');\r\n    }\r\n\r\n    $headerstring = '';\r\n    foreach ($headers as $header => $content) {\r\n        $headerstring .= $header.': '.$content.\"\\r\\n\";\r\n    }\r\n\r\n    $reqpath = $reqinfo['path'].(isset($reqinfo['query']) ? '?'.$reqinfo['query'] : '');\r\n\r\n    $opts = array('ssl' => array(\r\n            'verify_peer' => true,\r\n            'cafile' => '\/etc\/pki\/tls\/certs\/ca-bundle.crt'\r\n        ),\r\n        'http' => array(\r\n            'method' => $method,\r\n            'follow_location' => ($followlocation ? 1 : 0),\r\n            'ignore_errors' => true,\r\n            'timeout' => 15,\r\n            'header' => $headerstring\r\n        )\r\n    );\r\n    if (!empty($body)) $opts['http']['content'] = $body;\r\n    $context = stream_context_create($opts);\r\n\r\n    $stream = fopen($url, 'r', false, $context);\r\n    $meta = stream_get_meta_data($stream);\r\n    $content = stream_get_contents($stream);\r\n    if (!is_array($meta['wrapper_data'])) {\r\n        trigger_error(\"Invalid HTTP Header Response\");\r\n    }\r\n    $header = '';\r\n    \r\n    foreach($meta['wrapper_data'] as $header) {\r\n        if (strpos($header, ':') !== false) {\r\n            $line = explode(':', $header, 2);\r\n            if (strtolower($line[0]) == 'set-cookie') {\r\n                $cookie = explode(';', trim($line[1]), 2);\r\n                if (strpos($cookie[0], '=') !== false) {\r\n                    \/\/valid cookie\r\n                    $cdata = explode('=', $cookie[0], 2);\r\n                    $cookies[$cdata[0]] = $cdata[1];\r\n                }\r\n            }\r\n        }\r\n    }\r\n    fclose($stream);\r\n    if ($returnheader) $content = implode(\"\\r\\n\", $meta['wrapper_data']).\"\\r\\n\\r\\n\".$content;\r\n    return $content;\r\n}\r\n?&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u0e27\u0e31\u0e19\u0e01\u0e48\u0e2d\u0e19\u0e40\u0e08\u0e2d\u0e1a\u0e31\u0e4a\u0e01\u0e23\u0e30\u0e2b\u0e27\u0e48\u0e32\u0e07 curl + openssl + centos 6 \u0e19\u0e34\u0e14\u0e2b\u0e19\u0e48\u0e2d\u0e22 \u0e17\u0e35\u0e48\u0e17\u0e33\u0e43\u0e2b\u0e49 curl \u0e41\u0e25\u0e30 app \u0e17\u0e38\u0e01\u0e2d\u0e22\u0e48\u0e32\u0e07\u0e17\u0e35\u0e48\u0e43\u0e0a\u0e49 libcurl \u0e15\u0e48\u0e2d\u0e44\u0e1b\u0e2b\u0e32 server \u0e17\u0e35\u0e48\u0e40\u0e1b\u0e47\u0e19 https \u0e1a\u0e32\u0e07\u0e15\u0e31\u0e27\u0e1a\u0e19\u0e1a\u0e32\u0e07\u0e40\u0e07\u0e37\u0e48\u0e2d\u0e19\u0e44\u0e02\u0e44\u0e21\u0e48\u0e44\u0e14\u0e49 \u0e2b\u0e32\u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25\u0e44\u0e1b\u0e01\u0e47\u0e1e\u0e1a\u0e27\u0e48\u0e32\u0e15\u0e49\u0e2d\u0e07\u0e44\u0e1b\u0e41\u0e01\u0e49\u0e17\u0e35\u0e48\u0e1d\u0e31\u0e48\u0e07 server (\u0e08\u0e30\u0e1a\u0e49\u0e32\u0e40\u0e23\u0e2d\u0e30 server \u0e0a\u0e32\u0e27\u0e1a\u0e49\u0e32\u0e19) \u0e44\u0e21\u0e48\u0e01\u0e47\u0e15\u0e49\u0e2d\u0e07\u0e41\u0e01\u0e49 code \u0e43\u0e2b\u0e21\u0e48\u0e43\u0e2b\u0e49\u0e43\u0e0a\u0e49 library \u0e2d\u0e22\u0e48\u0e32\u0e07\u0e2d\u0e37\u0e48\u0e19\u0e41\u0e17\u0e19 \u0e01\u0e47\u0e40\u0e25\u0e22\u0e40\u0e01\u0e34\u0e14\u0e21\u0e32\u0e40\u0e1b\u0e47\u0e19 function \u0e19\u0e35\u0e49\u0e04\u0e23\u0e31\u0e1a \u0e21\u0e35 requirement \u0e19\u0e34\u0e14\u0e2b\u0e19\u0e48\u0e2d\u0e22\u0e04\u0e37\u0e2d PHP 5.3.4 \u0e02\u0e36\u0e49\u0e19\u0e44\u0e1b \u0e41\u0e25\u0e30 \u0e40\u0e1b\u0e34\u0e14 allow_url_fopen \u0e44\u0e27\u0e49\u0e43\u0e19 php.ini feature => custom header \u0e44\u0e14\u0e49 => \u0e23\u0e2d\u0e07\u0e23\u0e31\u0e1a cookie \u0e42\u0e14\u0e22\u0e2d\u0e31\u0e15\u0e42\u0e19\u0e21\u0e31\u0e15\u0e34 => method \u0e44\u0e14\u0e49\u0e17\u0e31\u0e49\u0e07 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-68488","post","type-post","status-publish","format-standard","hentry","category-misc"],"_links":{"self":[{"href":"https:\/\/www.icez.net\/blog\/wp-json\/wp\/v2\/posts\/68488","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.icez.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.icez.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.icez.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.icez.net\/blog\/wp-json\/wp\/v2\/comments?post=68488"}],"version-history":[{"count":2,"href":"https:\/\/www.icez.net\/blog\/wp-json\/wp\/v2\/posts\/68488\/revisions"}],"predecessor-version":[{"id":68491,"href":"https:\/\/www.icez.net\/blog\/wp-json\/wp\/v2\/posts\/68488\/revisions\/68491"}],"wp:attachment":[{"href":"https:\/\/www.icez.net\/blog\/wp-json\/wp\/v2\/media?parent=68488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.icez.net\/blog\/wp-json\/wp\/v2\/categories?post=68488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.icez.net\/blog\/wp-json\/wp\/v2\/tags?post=68488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}