0

I've been looking online for a few days and haven't been able to find a good example of posting data using php, I downloaded http_pecl and have tried to use it

Could you please help me understand what is wrong with my code? I believe it is that I am sending the data wrong so the server cannot see it (The server is expecting data the way that jquery post sends it)

$headers = array('method'  => 'POST','Content-type'  => 'application/json','content'=>json_encode(array('api_key'=>'5675476','grant_type' => 'password', 'username' => '12345','password'=>'12345')));

$r = new HttpRequest('http:/zztestapi.com/api/v5/authorize?api_key=5675476&grant_type=password&username=12345&password=12345', HttpRequest::METH_POST);
$r->setHeaders($headers);
$r->setContentType = 'application/json';
//$r->setBody(json_encode(array('api_key'=>'5675476','grant_type' => 'password',     'username' => '12345','password'=>'password')));
//$r->addPostFields(array('api_key'=>'5675476','grant_type' => 'password', 'username' => '12345','password'=>'password'));
//$r->addRawPostData(json_encode(array('api_key'=>'9000000141','grant_type' => 'password', 'username' => '12345','password'=>'password')));
$r->setOptions(array('cookies' => array('lang' => 'de')));
echo $r->send()->getBody(); 
$r->send();

My goal is to get code that sends similar to a jquery post

$.ajax({
  url: 'http://zztestapi.com/api/v5/authorize',
  type: 'POST',
  data: {"api_key":"5675476","grant_type":"password","username":"12345","password":"12345"},
  dataType: 'application/json',
  success: function(data) {
    alert(data);
  }
});
prog13
  • 1
  • 1
  • It Appears that using curl does not help solve the issue, I guess the data is still not seen as the same as data in a ajax json post – prog13 Nov 12 '13 at 18:54

1 Answers1

0

You can do it with cURL, here is this example:

$data = array("name" => "Hagrid", "age" => "36");                                                                    
$data_string = json_encode($data);                                                                                   

$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);

Hope this help!

gryphes
  • 27
  • 2
  • Thanks, I'll look into curl, is curl the only solution? I'm trying to figure out when it wont work with pecl as well – prog13 Nov 12 '13 at 17:55
  • You could try with **stream_context_create** [here](http://stackoverflow.com/a/14501952/2411728) is an example. – gryphes Nov 12 '13 at 18:37
  • I didn't realize but it looks like many versions php comes with curl included you just need to uncomment the php_curl extension in the php ini so I'm trying that out – prog13 Nov 12 '13 at 18:50