{"id":2548,"date":"2017-06-07T12:15:51","date_gmt":"2017-06-07T11:15:51","guid":{"rendered":"http:\/\/www.robertprice.co.uk\/robblog\/?p=2548"},"modified":"2017-06-07T12:15:51","modified_gmt":"2017-06-07T11:15:51","slug":"using-file_get_contents-put-json-data-https-webservice","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/using-file_get_contents-put-json-data-https-webservice\/","title":{"rendered":"Using file_get_contents to PUT JSON data over HTTPS to a webservice"},"content":{"rendered":"<p>It&#8217;s possible to use PHP&#8217;s <a href=\"http:\/\/php.net\/manual\/en\/function.file-get-contents.php\">file_get_contents<\/a> to PUT JSON data to a RESTful webservice if you don&#8217;t want to use cURL.<\/p>\n<p>In this example I&#8217;m going to try to add or update a person using the <a href=\"http:\/\/nationbuilder.com\/api_documentation\">NationBuilder API<\/a>.<\/p>\n<p>Firstly, we need the URL of the webservice, and the data you want to update as a JSON string.<\/p>\n<pre>\r\n$access_token = 'set access token here';\r\n$nation = 'set nation name here';\r\n$url = \"https:\/\/$nation.nationbuilder.com\/api\/v1\/people\/push?access_token=$access_token\";\r\n\/\/ details we want to update\r\n$data = [\r\n    'person' =&gt; [\r\n        'email_opt_in' =&gt; true,\r\n        'do_not_contact' =&gt; false,\r\n        'do_not_call' =&gt; false,\r\n        'first_name' =&gt; 'Robert',\r\n        'last_name' =&gt; 'Price',\r\n        'email' =&gt; 'robert.price@email.adress',\r\n        'home_address' =&gt; [\r\n            'zip' =&gt; 'AB1 2CD'\r\n        ],\r\n        'bio' =&gt; 'Software developer - robertprice.co.uk'\r\n    ]\r\n];\r\n$json_data = json_encode($data);\r\n<\/pre>\n<p>Now we need to setup a stream_context so file_get_contents knows how to handle the request. <\/p>\n<p>Even though we&#8217;re PUTing data over HTTPS, we need to use the http context. We set <code>method<\/code> to <code>PUT<\/code<. Our headers in this case we need to set the <code>Content-type<\/code> to <code>application\/json<\/code>, <code>Accept<\/code> to <code>application\/json<\/code>, <code>Connection<\/code> to <code>close<\/code>, and <code>Content-length<\/code> to the length of our JSON string. We&#8217;re PUTing this data over HTTP1.1 so we set the <code<protocol_version<\/code> to <code>1.1<\/code>. We&#8217;re sending the JSON in the body of the request, so we set this in the <code>content<\/code>.<\/p>\n<p>As we are using HTTPS, we need to configure the <code>ssl<\/code> settings. In this case I&#8217;m not going to verify the peer or the peer name.<\/p>\n<pre>\r\n$context = stream_context_create([\r\n    'http' =&gt; [\r\n        'method' =&gt; 'PUT',\r\n        'header' =&gt; \"Content-type: application\/json\\r\\n\" .\r\n                    \"Accept: application\/json\\r\\n\" .\r\n                    \"Connection: close\\r\\n\" .\r\n                    \"Content-length: \" . strlen($json_data) . \"\\r\\n\",\r\n        'protocol_version' =&gt; 1.1,\r\n        'content' =&gt; $json_data\r\n    ],\r\n    'ssl' =&gt; [\r\n        'verify_peer' =&gt; false,\r\n        'verify_peer_name' =&gt; false\r\n    ]\r\n]);\r\n<\/pre>\n<p>Finally we PUT the data by calling file_get_contents. NationBuilder returns a JSON response to this call, so we can decode that and echo a confirmation message to the screen.<\/p>\n<pre>\r\n$rawdata = file_get_contents($url, false, $context);\r\nif ($rawdata === false) {\r\n    exit(\"Unable to update data at $url\");\r\n}\r\n$data = json_decode($rawdata, true);\r\nif (JSON_ERROR_NONE !== json_last_error()) {\r\n    exit(\"Failed to parse json: \" . json_last_error_msg());\r\n}\r\necho \"Updated details for {$data['person']['first_name']} {$data['person']['last_name']}\\n\";\r\n<\/pre>\n<p>The full code for this, and some other examples of querying NationBuilder using PHP, can be found in my <a href=\"https:\/\/github.com\/robertprice\/nationbuilder-people-in-nation\">NationBuilder People In Nation &#8211; GitHub repository<\/a>.<\/p>\n<p>You may also be interested in my previous article on <a href=\"\/robblog\/posting-json-to-a-web-service-with-php\/\">POSTing JSON to Web Service<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s possible to use PHP&#8217;s file_get_contents to PUT JSON data to a RESTful webservice if you don&#8217;t want to use cURL. In this example I&#8217;m going to try to add or update a person using the NationBuilder API. Firstly, we need the URL of the webservice, and the data you want to update as a &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/using-file_get_contents-put-json-data-https-webservice\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using file_get_contents to PUT JSON data over HTTPS to a webservice&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[50,76],"class_list":["post-2548","post","type-post","status-publish","format-standard","hentry","category-dev","tag-php","tag-web-service"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Using file_get_contents to PUT JSON data over HTTPS to a webservice - Robert Price<\/title>\n<meta name=\"description\" content=\"An example of using PHP&#039;s file_get_contents to PUT data over HTTPS to the Nationbuilder RESTful API, without having to use cURL.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/using-file_get_contents-put-json-data-https-webservice\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using file_get_contents to PUT JSON data over HTTPS to a webservice - Robert Price\" \/>\n<meta property=\"og:description\" content=\"An example of using PHP&#039;s file_get_contents to PUT data over HTTPS to the Nationbuilder RESTful API, without having to use cURL.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/using-file_get_contents-put-json-data-https-webservice\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2017-06-07T11:15:51+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-file_get_contents-put-json-data-https-webservice\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-file_get_contents-put-json-data-https-webservice\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Using file_get_contents to PUT JSON data over HTTPS to a webservice\",\"datePublished\":\"2017-06-07T11:15:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-file_get_contents-put-json-data-https-webservice\\\/\"},\"wordCount\":249,\"keywords\":[\"PHP\",\"Web Service\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-file_get_contents-put-json-data-https-webservice\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-file_get_contents-put-json-data-https-webservice\\\/\",\"name\":\"Using file_get_contents to PUT JSON data over HTTPS to a webservice - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2017-06-07T11:15:51+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"description\":\"An example of using PHP's file_get_contents to PUT data over HTTPS to the Nationbuilder RESTful API, without having to use cURL.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-file_get_contents-put-json-data-https-webservice\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-file_get_contents-put-json-data-https-webservice\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-file_get_contents-put-json-data-https-webservice\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using file_get_contents to PUT JSON data over HTTPS to a webservice\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using file_get_contents to PUT JSON data over HTTPS to a webservice - Robert Price","description":"An example of using PHP's file_get_contents to PUT data over HTTPS to the Nationbuilder RESTful API, without having to use cURL.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/using-file_get_contents-put-json-data-https-webservice\/","og_locale":"en_GB","og_type":"article","og_title":"Using file_get_contents to PUT JSON data over HTTPS to a webservice - Robert Price","og_description":"An example of using PHP's file_get_contents to PUT data over HTTPS to the Nationbuilder RESTful API, without having to use cURL.","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/using-file_get_contents-put-json-data-https-webservice\/","og_site_name":"Robert Price","article_published_time":"2017-06-07T11:15:51+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-file_get_contents-put-json-data-https-webservice\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-file_get_contents-put-json-data-https-webservice\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Using file_get_contents to PUT JSON data over HTTPS to a webservice","datePublished":"2017-06-07T11:15:51+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-file_get_contents-put-json-data-https-webservice\/"},"wordCount":249,"keywords":["PHP","Web Service"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-file_get_contents-put-json-data-https-webservice\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/using-file_get_contents-put-json-data-https-webservice\/","name":"Using file_get_contents to PUT JSON data over HTTPS to a webservice - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2017-06-07T11:15:51+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"description":"An example of using PHP's file_get_contents to PUT data over HTTPS to the Nationbuilder RESTful API, without having to use cURL.","breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-file_get_contents-put-json-data-https-webservice\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/using-file_get_contents-put-json-data-https-webservice\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-file_get_contents-put-json-data-https-webservice\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Using file_get_contents to PUT JSON data over HTTPS to a webservice"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2548","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=2548"}],"version-history":[{"count":1,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2548\/revisions"}],"predecessor-version":[{"id":2549,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2548\/revisions\/2549"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=2548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=2548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=2548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}