{"id":1416,"date":"2013-02-01T07:54:13","date_gmt":"2013-02-01T07:54:13","guid":{"rendered":"http:\/\/www.robertprice.co.uk\/robblog\/?p=1416"},"modified":"2017-05-31T14:12:57","modified_gmt":"2017-05-31T13:12:57","slug":"posting-json-to-a-web-service-with-php","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/posting-json-to-a-web-service-with-php\/","title":{"rendered":"POSTing JSON To A Web Service With PHP"},"content":{"rendered":"<p>I needed to POST JSON formatted data to a RESTful web service using PHP earlier, so I thought I&#8217;d share my solution.<\/p>\n<p>There are a couple of approaches that could be taken, using the CURL extension, or <a href=\"http:\/\/php.net\/manual\/en\/function.file-get-contents.php\">file_get_contents<\/a> and a <a href=\"http:\/\/php.net\/manual\/en\/context.http.php\">http context<\/a>. I took the later way.<\/p>\n<p>When POSTing JSON to a RESTful web service we don&#8217;t send post fields or pretend to be a form, instead we have to send the JSON data in body of the request, and let the web service know it&#8217;s JSON data by setting the Content-type header to application\/json. <\/p>\n<pre class=\"lang:php decode:true \" title=\"Example of POSTing JSON data to a web service in PHP\" >$article = new stdClass();\r\n$article-&gt;title = \"An example article\";\r\n$article-&gt;summary = \"An example of posting JSON encoded data to a web service\";\r\n\r\n$json_data = json_encode($article);\r\n\r\n$post = file_get_contents('http:\/\/localhost\/rest\/articles',null,stream_context_create(array(\r\n    'http' =&gt; array(\r\n        'protocol_version' =&gt; 1.1,\r\n        'user_agent'       =&gt; 'PHPExample',\r\n        'method'           =&gt; 'POST',\r\n        'header'           =&gt; \"Content-type: application\/json\\r\\n\".\r\n                              \"Connection: close\\r\\n\" .\r\n                              \"Content-length: \" . strlen($json_data) . \"\\r\\n\",\r\n        'content'          =&gt; $json_data,\r\n    ),\r\n)));\r\n\r\nif ($post) {\r\n    echo $post;\r\n} else {\r\n    echo \"POST failed\";\r\n}\r\n<\/pre>\n<p>Here I&#8217;m first creating an example PHP <code>stdClass<\/code> object, populating it with some data, then serialising it to a JSON string. The real magic is using <code>file_get_contents<\/code> to POST it over HTTP to my test web service. If the POST succeeds, then it&#8217;s displayed, else an error message is shown.<\/p>\n<p>It&#8217;s important to note I send the header, <code>Connection: close<\/code> . Without this, your script will hang until the web server closes the connection. If we include it, then as soon as the data has POSTed the connection is closed and control returned to the script.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I needed to POST JSON formatted data to a RESTful web service using PHP earlier, so I thought I&#8217;d share my solution. There are a couple of approaches that could be taken, using the CURL extension, or file_get_contents and a http context. I took the later way. When POSTing JSON to a RESTful web service &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/posting-json-to-a-web-service-with-php\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;POSTing JSON To A Web Service With PHP&#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":[25,34,50,76],"class_list":["post-1416","post","type-post","status-publish","format-standard","hentry","category-dev","tag-http","tag-json","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>POSTing JSON To A Web Service Using PHP<\/title>\n<meta name=\"description\" content=\"An example of POSTing JSON data to a RESTful web service using PHP. The example code shows the use of PHP&#039;s file_get_contents method over HTTP.\" \/>\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\/posting-json-to-a-web-service-with-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"POSTing JSON To A Web Service Using PHP\" \/>\n<meta property=\"og:description\" content=\"An example of POSTing JSON data to a RESTful web service using PHP. The example code shows the use of PHP&#039;s file_get_contents method over HTTP.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/posting-json-to-a-web-service-with-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2013-02-01T07:54:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-05-31T13:12:57+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\\\/posting-json-to-a-web-service-with-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/posting-json-to-a-web-service-with-php\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"POSTing JSON To A Web Service With PHP\",\"datePublished\":\"2013-02-01T07:54:13+00:00\",\"dateModified\":\"2017-05-31T13:12:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/posting-json-to-a-web-service-with-php\\\/\"},\"wordCount\":203,\"keywords\":[\"HTTP\",\"JSON\",\"PHP\",\"Web Service\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/posting-json-to-a-web-service-with-php\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/posting-json-to-a-web-service-with-php\\\/\",\"name\":\"POSTing JSON To A Web Service Using PHP\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2013-02-01T07:54:13+00:00\",\"dateModified\":\"2017-05-31T13:12:57+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"description\":\"An example of POSTing JSON data to a RESTful web service using PHP. The example code shows the use of PHP's file_get_contents method over HTTP.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/posting-json-to-a-web-service-with-php\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/posting-json-to-a-web-service-with-php\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/posting-json-to-a-web-service-with-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"POSTing JSON To A Web Service With PHP\"}]},{\"@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":"POSTing JSON To A Web Service Using PHP","description":"An example of POSTing JSON data to a RESTful web service using PHP. The example code shows the use of PHP's file_get_contents method over HTTP.","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\/posting-json-to-a-web-service-with-php\/","og_locale":"en_GB","og_type":"article","og_title":"POSTing JSON To A Web Service Using PHP","og_description":"An example of POSTing JSON data to a RESTful web service using PHP. The example code shows the use of PHP's file_get_contents method over HTTP.","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/posting-json-to-a-web-service-with-php\/","og_site_name":"Robert Price","article_published_time":"2013-02-01T07:54:13+00:00","article_modified_time":"2017-05-31T13:12:57+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\/posting-json-to-a-web-service-with-php\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/posting-json-to-a-web-service-with-php\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"POSTing JSON To A Web Service With PHP","datePublished":"2013-02-01T07:54:13+00:00","dateModified":"2017-05-31T13:12:57+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/posting-json-to-a-web-service-with-php\/"},"wordCount":203,"keywords":["HTTP","JSON","PHP","Web Service"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/posting-json-to-a-web-service-with-php\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/posting-json-to-a-web-service-with-php\/","name":"POSTing JSON To A Web Service Using PHP","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2013-02-01T07:54:13+00:00","dateModified":"2017-05-31T13:12:57+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"description":"An example of POSTing JSON data to a RESTful web service using PHP. The example code shows the use of PHP's file_get_contents method over HTTP.","breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/posting-json-to-a-web-service-with-php\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/posting-json-to-a-web-service-with-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/posting-json-to-a-web-service-with-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"POSTing JSON To A Web Service With PHP"}]},{"@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\/1416","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=1416"}],"version-history":[{"count":3,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/1416\/revisions"}],"predecessor-version":[{"id":1574,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/1416\/revisions\/1574"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=1416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=1416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=1416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}