json - How to create the REST API for this curl operation in PHP -
I want to execute through php this simple curl operation I can execute this in simple php Somebody Framework not simple php or do I need a framework?
curl -XPOST localhost: 12060 / stores / create schema / FieldType H "Content-Type: application / json '-d' {action:" ", fieldType: {name:" n $ name "ValueType: {primitive:" STRING "}, scope:" versioned "namespace: {" my.demo ":" n "}}} 'D - I The one who tried it is:
& lt ;? Php $ url = "localhost: 12060 / repository / schema / field type"; // open connection $ option = array (CURLOPT_RETURNTRANSFER = & gt; true, // return web page CURLOPT_HEADER => false, // not return header CURLOPT_FOLLOWLOCATION = & gt; true, // follow redirects CURLOPT_ENCODING = & gt; ; ""; // all encoding handle CURLOPT_USERAGENT = & gt; "Spider", I // Who I CURLOPT_AUTOREFERER = & gt; 120, // connect timeout on CURLOPT_TIMEOUT = & gt; true, // redirect CURLOPT_CONNECTTIMEOUT = & gt Set referenced at 120, stop the CURLOPT_MAXREDIRS => 10, // 10 after redirections on the timeout response); $ Ch = curl_init (); // set URL, number of post wars, post data curl_setopt ($ ch, CURLOPT_URL, $ url); $ FieldString = array ( "action" = & gt; "fieldType" "making of", = & gt; array ( "name" = & gt; "n $ name", "ValueType" = & gt; array ( "primitive "= & Gt;" STRING ")," Scope "= & gt;" Version "," Namespace "=> Array (" my.demo "=> gt; n))); // some diff curl_setopt ($ ch, CURLOPT_HTTPHEADERS, array ('content-type: application / jason')); Curl_setopt ($ CH, CURLOPT_SSL_VERIFYPEER, incorrect); Curl_setopt ($ CH, CURLOPT_SSL_VERIFYHOST, 0); Curl_setopt ($ ch, CURLOPT_VERBOSE, TRUE); Curl_setopt ($ ch, CURLOPT_POSTFIELDS, json_encode ("{json: $ fieldString}")); Curl_setopt ($ CH, CURLOPT_POST, 1); Curl_setopt_array ($ ch, $ option); // execute post $ result = curl_exec ($ ch); $ Header = curl_getinfo ($ ch); $ Return result; // close connection curl_close ($ ch); ? & Gt; but it is giving me this
The given resource version is not supported. Please use one of the following: * version [mediaType = application / json, language = null, encoding = null]
your problem is here:
curl_setopt ($ ch, CURLOPT_POSTFIELDS, json_encode (" {json: $ fieldString} ")); $ field string is not really a string because its name means that it was still an array at this point and you have a suspicious faux JSN string Trying to re-coded, only in the form of the array data. Instead, use this to get the desired effect (?):
curl_setopt ($ ch, CURLOPT_POSTFIELDS, json_encode ($ fieldString));
Comments
Post a Comment