realeez Posted September 28, 2013 Share Posted September 28, 2013 Dear pals, I am newbie in RESTful services . I need to call a GET Verb in Server . I know 2 methods 1. Ajax Call we can write it as $.ajax({ url: url, dataType: "html", type: 'GET', data: "id="+id+"&type="+type, success: function(data){ //$("#content").html(data); alert(data); $('table #sample-boxed-2-pagination th a').each(function(){ //this.href = this.href.replace(sub_url, main_url); var value = this.href.split('?'); //alert(value[0]); if(value[0]!=sub_url) { this.href = this.href.replace(value[0], sub_url); } }); } }); }); But I know it's not working in Cross domain scenario . Please advise a method to work same in all domains . 2. Using file_get_contents() function like $response = file_get_contents('https://kkl.com/graph/call?parm1=9'); I know I can call POST verb using cURL as $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://localhost/simple_rest_master/test"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); $data = array( 'username' => 'foo', 'password' => 'bar' ); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $contents = curl_exec($ch); curl_close($ch); echo $contents; // manipulate response Do you can advise the syntax of GET call using cURL ? Waiting your fast reply Thanks, Anes Link to comment https://forums.phpfreaks.com/topic/282504-how-to-handle-rest-api-call-for-get-verb/ Share on other sites More sharing options...
kicken Posted September 28, 2013 Share Posted September 28, 2013 Do you can advise the syntax of GET call using cURL ? Just leave out the bit where you are telling it to do a POST instead: curl_setopt($ch, CURLOPT_POST, 1); If you don't tell it you want a POST done with that line, then it will do a GET request. Link to comment https://forums.phpfreaks.com/topic/282504-how-to-handle-rest-api-call-for-get-verb/#findComment-1451590 Share on other sites More sharing options...
trq Posted September 28, 2013 Share Posted September 28, 2013 You might also want to have a look at Guzzle, it makes http so much easier / cleaner. Link to comment https://forums.phpfreaks.com/topic/282504-how-to-handle-rest-api-call-for-get-verb/#findComment-1451599 Share on other sites More sharing options...
realeez Posted September 29, 2013 Author Share Posted September 29, 2013 @kick,@trq Thanks for your advise dears. I figure out my solution as below: $ch = curl_init(); $data = array( 'username' => 'foobar', 'password' => 'bar' ); curl_setopt($ch, CURLOPT_URL, "http://localhost/simple_rest_master/test?".http_build_query($data)); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPGET ,1); $contents = curl_exec($ch); curl_close($ch); echo $contents; Thanks, Anes Link to comment https://forums.phpfreaks.com/topic/282504-how-to-handle-rest-api-call-for-get-verb/#findComment-1451624 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.