0

I need to convert this "normal" curl string to php curl. This command is tested to work from shell:

curl -XPUT http://foo/monitors/1.json -d "Monitor[Name]=test1"

I have looked at a lot of tuts and examples, but my attempts so far have been fruitless. Here's the latest i tried:

$data = array("Name" => "test1");
$payload = json_encode( array( "Monitor"=> $data ) );
$url = 'hxxp://foo/monitors/1.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS,$payload);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch) 

also the json I'm trying to put to is formatted like this:

{"monitor":{"Monitor":{"Id":"1","Name":"test","Type":"Remote","Funct.....

thanks

*Updated with more current info

  • pls, refer also [duplicate](http://stackoverflow.com/questions/11079135/how-to-post-json-data-with-php-curl) question. – kuldipem Dec 28 '14 at 03:32
  • sorry I not sure what you mean? – voldsomenterprise Dec 28 '14 at 03:40
  • there is one question which is same as you asked, which can help you to solve your problem. question link is [question](http://stackoverflow.com/questions/11079135/how-to-post-json-data-with-php-curl) – kuldipem Dec 28 '14 at 03:47
  • okay, I have looked at these and now tried this but no luck: `code` $data = array("Name" => "test1"); $payload = json_encode( array( "Monitor"=> $data ) ); $url = 'http://foo/monitors/1.json' $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_POSTFIELDS,$payload); curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch) `code` – voldsomenterprise Dec 28 '14 at 04:05
  • What happened ? any error ? – kuldipem Dec 28 '14 at 04:24
  • nothing happened, Name value was not updated and no error. – voldsomenterprise Dec 28 '14 at 04:41
  • can you tell me how you are getting response and parse at url ? – kuldipem Dec 28 '14 at 04:46
  • Not sure what you mean but this command works from shell: `code`curl -XPUT hxxp://foo/monitors/1.json -d "Monitor[Name]=test1" `code` and I'm able to get json and xml responses from the api in my browser – voldsomenterprise Dec 28 '14 at 04:53

2 Answers2

0
    $data_string =""/* your json string goes here like {"monitor":{"Monitor":{"Id":"1","Name":"test","Type":"Remote"}} */                                                                             

    $ch = curl_init('http://api.local/rest/users');                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($data_string))                                                                       
    );                                                                                                                   

    $result = curl_exec($ch);
kuldipem
  • 1,719
  • 1
  • 19
  • 32
0

the equivalent of your command line sample is really:

$payload = "Monitor[Name]=test1";
$url = 'http://localhost/monitors/1.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS,$payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

in that your sample does not use "application/json" as the "Content-Type" but actually the stock "application/x-www-form-urlencoded" value and the "data" is not really JSON

Hans Z.
  • 50,496
  • 12
  • 102
  • 115