Posting a JSON object to a REST API using PHP -


i have found couple of helpful links on stackoverflow haven't helped me complete task because complete beginner in trying write php or use curl etc.

send json post using php

posting json data api using curl

i have been using postman in chrome test api calls want put demo system on apache web server.

does have example of php webform posting json object rest api?

here example of want send:

<?php $url = "https://api.url/api/v1//accounts/create"; $jdata = json_encode($data); $data = [{   "status": "active",   "username": ["uname"],   "password": ["pword"],   "attributes": {     "forenames": ["fname"],     "surname": ["lname"],     "emailaddress": ["email"]                  },           }] ?> 

any advice fantastic. said, new curl , php, unfamiliar array approach mentioned in other articles , ["info"] elements should populated information filled in on webform.

i hope have been concise , explanitory please let me know if need anymore information.

snook

try following, modifying steps 1 , 2 accordingly:

function sendrequest($data,$url) {     $postdata = http_build_query(array('data'=>$data));     $opts = array('http' =>       array(           'method'  => 'post',           'header'  => "content-type: application/x-www-form-urlencoded \r\n",                        //"x-requested-with: xmlhttprequest \r\n".                        //"curl/7.9.8 (i686-pc-linux-gnu) libcurl 7.9.8 (openssl 0.9.6b) (ipv6 enabled)\r\n",           'content' => $postdata,           'ignore_errors' => true,           'timeout' =>  10,       )     );     $context  = stream_context_create($opts);     return file_get_contents($url, false, $context); }   // 1.- add json $data = '[{     "status"      : "active",     "username"    : ["uname"],     "password"    : ["pword"],     "attributes"  : {         "forenames"   : ["fname"],         "surname"     : ["lname"],         "emailaddress": ["email"]     }, }]';  // 2.- add api endpoint $url= "https://api.url/api/v1//accounts/create";   // 3.- fire $result = sendrequest($data,$url);  // 4.- dump result echo $result; die(); 

good luck!!


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -