POST CURL to JSON using PHP (use Cookie.txt) -
I want to post some curls in a REST API with the use of an PHP forwarding. A brief overview of my project ... User (We call him Anton) Log on to the classic logon form on the Starsite after which the user should be logged in until he wants to logout (but here's the problem) The user's main Work is to fill the form with project name and description. After submitting, the data is transferred to the JSON object which creates a project in the software. When I run the curl command manually through the terminal, everything works fine but my PHP script is unfortunately not so.
log works fine in terminal
curl -v \ -c cookie.txt \ -X Post \ F j_username = Anton \ F j_password = pw \ http : //test.com/login.json
log in PHP
works fine in lt ;? Php // log in error_reporting (E_ALL | E_STRICT); Ini_set ('display_errors', '1'); $ Ch = curl_init (); Curl_setopt ($ c, CURLOPT_URL, "http://test.com/login.json"); Curl_setopt ($ CH, CURLOPT_RETURNTRANSFER, 1); Curl_setopt ($ CH, CURLOPT_FOLLOWLOCATION, 1); Curl_setopt ($ ch, CURLOPT_COOKIEJAR, dirname (__file__). '/ Cookie.txt'); Curl_setopt ($ ch, CURLOPT_COOKIEFILE, dirname (__file__). '/ Cookie.txt'); Curl_setopt ($ ch, CURLOPT_POST, true); $ Data = array ('j_username' = & gt; 'aton', 'j_password' => gt; pw '); Curl_setopt ($ CH, CURLOPT_POSTFIELDS, $ data); $ Output = curl_xac ($ CH); $ Info = curl_getinfo ($ ch); Curl_close ($ ch); ? & Gt;
** Prepare the project works fine in the terminal, but do not create a project in php ... I do not know at all! How do I use cookie.txt and how do I fill array (-D part) with data?
... and how can I stay logged in to create a project?
curl -v \ b cookie.txt \ -X post \ h "content type: application / json" \ -d '{"name": "name12345", "description": " Some details type "} '\ http://test.com/productions.json
Edit ... but does it really work? I have trouble using cookie.txt: php // // output function post_to_url ($ url, $ data) {$ fields = '' create, (
& lt foreach ($ Data in the form of $ key = & gt; {. $ Sectors = $ key '=' $ value ... '& amp; amp; nbsp;} $ value) rtrim ($ fields,' & amp; '); $ post = curl_init (); curl_setopt ($ post, CURLOPT_URL, $ url); curl_setopt ($ post, CURLOPT_COOKIEFILE, dirname (__ file __) '/ cookie.txt'.); curl_setopt ($ post, CURLOPT_COOKIEJAR, dirname (__ file __) '/ cookie.txt'.); curl_setopt ($ post, CURLOPT_POST, ($ data) count); curl_setopt ($ post, CURLOPT_POSTFIELDS, $ fields); curl_setopt ($ W Trust, CURLOPT_RETURNTRANSFER, 1); $ result = curl_exec ($ post); curl_close ($ post);} $ Data = array ( "name" = & gt; "max", "description" = & gt; "of some sort Details "); post_to_url (" http://test.com/productions.json ", $ data); Print_r ($ data);? & Gt;
I trust your help Thank you!
if someone can translate this curl post then you are very There should be a lot of ...
curl -v \ -b cookie.txt \ -X POST \ -H "content-type: application / jsan" \ -d "{" name ":" Name 12345 "," Details ":" Some types of v Rann "} '\ http://test.com/productions.json
... in PHP with the use of Siaruar_stiopiti.
Thanks a lot!
Comments
Post a Comment