{"id":424,"date":"2014-04-02T23:01:19","date_gmt":"2014-04-02T23:01:19","guid":{"rendered":"http:\/\/nikola-breznjak.com\/blog\/?p=424"},"modified":"2015-08-20T11:35:14","modified_gmt":"2015-08-20T11:35:14","slug":"post-json-data-with-php-curl-wrapper-class","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/php\/post-json-data-with-php-curl-wrapper-class\/","title":{"rendered":"POST JSON Data With PHP cURL wrapper class"},"content":{"rendered":"<p>PHP <a href=\"http:\/\/nikola-breznjak.com\/blog\/php\/post-json-data-with-php-curl-wrapper-class\/\">biceps <\/a>cURL \ud83d\ude42<a href=\"http:\/\/nikola-breznjak.com\/blog\/php\/post-json-data-with-php-curl-wrapper-class\/\"><br \/>\n<\/a><\/p>\n<p>Following is the PHP cURL wrapper class which I use to make GET and POST requests. The examples are below. Disclamer: \u00a0be sure that you have permission to scrape and use the content you&#8217;re after.<\/p>\n<p>This code is also available on GitHub at this link:\u00a0<a href=\"https:\/\/github.com\/Hitman666\/PHPcURLWrapper\">https:\/\/github.com\/Hitman666\/PHPcURLWrapper<\/a><\/p>\n<h3>Class code and simple GET request<\/h3>\n<pre class=\"lang:default decode:true\">\/\/CurlWrapper_static.php\r\n\r\nclass CurlWrapper {\r\n    private static $useragents = array(            \r\n        \"Mozilla\/5.0 (Windows NT 6.1; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/33.0.1750.154 Safari\/537.36\",\r\n        \"Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.0; WOW64; Trident\/4.0; SLCC1)\",\r\n        \"Mozilla\/5.0 (Windows NT 6.2; WOW64; rv:28.0) Gecko\/20100101 Firefox\/28.0\",\r\n        \"Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit\/537.75.14 (KHTML, like Gecko) Version\/7.0.3 Safari\/537.75.14\"\r\n    );\r\n\r\n\tprivate static $cookiesFile = \"curlCookies.txt\";\r\n\r\n    private static function getUserAgent() {\r\n    \t$rand = rand(0, count(self::$useragents) - 1);\r\n\r\n    \treturn self::$useragents[$rand];\r\n    }\r\n\r\n    public static function SendRequest($url, $ref = \"\", $type = \"GET\", $postData = \"\", $headers = \"\", $proxy = \"\") {\r\n        $useragent = self::getUserAgent();\r\n\r\n        $ch = curl_init();\r\n\t\tcurl_setopt($ch, CURLOPT_URL,$url);\r\n\t\tcurl_setopt($ch, CURLOPT_TIMEOUT,120);\r\n\t\tcurl_setopt($ch, CURLOPT_USERAGENT, self::getUserAgent());\r\n\r\n\t\tcurl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); \r\n\t\tcurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\r\n\t\tcurl_setopt($ch, CURLOPT_AUTOREFERER, true);\r\n\r\n\t\tcurl_setopt($ch, CURLOPT_COOKIEJAR, realpath(self::$cookiesFile)); \r\n\t\tcurl_setopt($ch, CURLOPT_COOKIEFILE, realpath(self::$cookiesFile));\r\n\r\n\t\tcurl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);\r\n\r\n        \/\/options\r\n        if ($ref != \"\") {\r\n            curl_setopt($ch, CURLOPT_REFERER, $ref);\r\n        }\r\n\r\n\t\tif ($proxy != \"\") {\r\n\t\t\tcurl_setopt($ch, CURLOPT_PROXY, $proxy);\r\n\t\t}\r\n\r\n\t\tif ($type == \"POST\"){\r\n\t\t\tcurl_setopt($ch, CURLOPT_CUSTOMREQUEST, \"POST\");\r\n\t\t\tcurl_setopt($ch, CURLOPT_POST, true);\r\n\t\t\tcurl_setopt($ch, CURLOPT_POSTFIELDS, $postData);\r\n\t\t}\r\n\r\n\t\tif ($headers != \"\"){\r\n\t\t\tcurl_setopt($ch, CURLOPT_HTTPHEADER, $headers);\r\n\t\t}\r\n\r\n        $result = curl_exec($ch);\r\n\t\tcurl_close($ch);\r\n\r\n\t\treturn $result;\r\n\t}\r\n}<\/pre>\n<p>Simple GET request:<\/p>\n<pre class=\"lang:default decode:true\">require(\"CurlWrapper_static.php\");\r\n\r\n$googleHTML = CurlWrapper::SendRequest('https:\/\/www.google.com');\r\necho $googleHTML;<\/pre>\n<p>If you&#8217;re a firm non-static lover here&#8217;s a &#8220;normal&#8221; class:<\/p>\n<pre class=\"lang:default decode:true\">\/\/CurlWrapper_nonStatic.php\r\n\r\nclass CurlWrapper{    \r\n    private $_useragents = array(            \r\n        \"Mozilla\/5.0 (Windows NT 6.1; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/33.0.1750.154 Safari\/537.36\",\r\n        \"Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.0; WOW64; Trident\/4.0; SLCC1)\",\r\n        \"Mozilla\/5.0 (Windows NT 6.2; WOW64; rv:28.0) Gecko\/20100101 Firefox\/28.0\",\r\n        \"Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit\/537.75.14 (KHTML, like Gecko) Version\/7.0.3 Safari\/537.75.14\"\r\n    );\r\n\r\n\tprivate $_cookiesFile = \"curlCookies.txt\";\r\n\r\n    private function getUserAgent(){\r\n    \t$rand = rand(0, count($this-&gt;_useragents));\r\n\r\n    \treturn $useragents[$rand];\r\n    }\r\n\r\n    public function SendRequest($url, $ref = \"\", $type = \"GET\", $postData = \"\", $headers = \"\", $proxy = \"\") {\r\n        $useragent = $this-&gt;getUserAgent();\r\n        echo $useragent;\r\n\r\n        $ch = curl_init();\r\n\t\tcurl_setopt($ch, CURLOPT_URL,$url);\r\n\t\tcurl_setopt($ch, CURLOPT_TIMEOUT,120);\r\n\t\tcurl_setopt($ch, CURLOPT_USERAGENT, $useragent);\r\n\r\n\t\tcurl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); \r\n\t\tcurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\r\n\t\tcurl_setopt($ch, CURLOPT_AUTOREFERER, true);\r\n\r\n\t\tcurl_setopt($ch, CURLOPT_COOKIEJAR, realpath($this-&gt;_cookiesFile)); \r\n\t\tcurl_setopt($ch, CURLOPT_COOKIEFILE, realpath($this-&gt;_cookiesFile));\r\n\r\n\t\tcurl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);\r\n\r\n        \/\/options\r\n        if ($ref != \"\") {\r\n            curl_setopt($ch, CURLOPT_REFERER, $ref);\r\n        }\r\n\r\n\t\tif ($proxy != \"\") {\r\n\t\t\tcurl_setopt($ch, CURLOPT_PROXY, $proxy);\r\n\t\t}\r\n\r\n\t\tif ($type == \"POST\"){\r\n\t\t\tcurl_setopt($ch, CURLOPT_CUSTOMREQUEST, \"POST\");\r\n\t\t\tcurl_setopt($ch, CURLOPT_POST, true);\r\n\t\t\tcurl_setopt($ch, CURLOPT_POSTFIELDS, $postData);\r\n\t\t}\r\n\r\n\t\tif ($headers != \"\"){\r\n\t\t\tcurl_setopt($ch, CURLOPT_HTTPHEADER, $headers);\r\n\t\t}\r\n\r\n        $result = curl_exec($ch);\r\n\t\tcurl_close($ch);\r\n\r\n\t\treturn $result;\r\n\t}\r\n}<\/pre>\n<p>How to use it with simple GET request:<\/p>\n<pre class=\"lang:default decode:true\">require(\"CurlWrapper_nonStatic.php\");\r\n\r\n$curl = new CurlWrapper();\r\n$googleHTML = $curl-&gt;SendRequest('https:\/\/www.google.com');\r\necho $googleHTML;<\/pre>\n<h3>JSON POST request<\/h3>\n<p>Here&#8217;s an example of sending a JSON POST request to imaginitive URL &#8216;http:\/\/service.com\/getData.json&#8217; with some data array:<\/p>\n<pre class=\"lang:default decode:true\">\trequire(\"CurlWrapper_static.php\");\r\n\r\n\t$cookieSettingUrl = 'http:\/\/service.com\/';\r\n\t$cookie = CurlWrapper::SendRequest($cookieSettingUrl);\r\n\r\n\t$data = array(\r\n\t\t\"year\" =&gt; 2014,\r\n\t\t\"day\" =&gt; 3,\r\n                \"month\" =&gt; 4,\r\n\t\t\"id\" =&gt; 20\r\n    );\r\n\t$postData = json_encode($data);\r\n\r\n\t$jsonUrl = 'http:\/\/service.com\/getData.json';\r\n\t$headers = array('Accept: application\/json','Content-Type: application\/json');\r\n\r\n\t$resultsHTML = CurlWrapper::SendRequest($jsonUrl, $cookieSettingUrl, \"POST\", $postData, $headers);\r\n\t$resultsJson = json_decode($resultsHTML);\r\n\tvar_dump($resultsJson);<\/pre>\n<p>Important to note is that you have to add proper $headers array, and that you json_encode your data array as shown when POSTing to a service which expects JSON data.<\/p>\n<p><span style=\"font-family: Bitter, Georgia, serif; font-size: 22px; line-height: 1.3;\">Cookies<br \/>\n<\/span><\/p>\n<p>The reason why I first used these two lines:<\/p>\n<pre class=\"lang:default decode:true\">$cookieSettingUrl = 'http:\/\/service.com\/';\r\n$cookie = CurlWrapper::SendRequest($cookieSettingUrl);<\/pre>\n<p>is to set any cookies (and you will find that some services do this) that may be needed to be sent along with the request to &#8216;http:\/\/service.com\/getData.json&#8217;. Cookies are set to &#8216;curlCookies.txt&#8217; in the CurlWrapper_* class. You can change this to your liking, and you have to make sure that you set proper permissions for this file.<\/p>\n<p>Hope this proves useful to someone.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP biceps cURL \ud83d\ude42 Following is the PHP cURL wrapper class which I use to make GET and POST requests. The examples are below. Disclamer: \u00a0be sure that&hellip;<\/p>\n","protected":false},"author":1,"featured_media":426,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,3],"tags":[],"class_list":["post-424","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-codeproject","category-php"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/424","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=424"}],"version-history":[{"count":7,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/424\/revisions"}],"predecessor-version":[{"id":432,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/424\/revisions\/432"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/426"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}