I am trying to test client server with php and json. I am trying curl json request to an api url, and its seems it works.
Here is client code :
$data = array('id' => 2, 'something' => 4);
$content = json_encode($data);
$url = 'http://localhost/json.api.php';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
$response = json_decode($result);
echo "<pre>";
var_dump($response);
curl_close($curl);
But, how can i get this request on server (on json.api.php) and parse it do something and then return json to requester client.
you can use $HTTP_RAW_POST_DATA to get the request
Related
I have a Rest api which I can access by this url: "http://127.0.0.1:8000/api/thesis/?format=json". Now I want to get the JSON data from it. For connecting to the api I tried to use PHP-Curl as below. But I get NULL! (This is the first time I'm doing php any help will be great!)
<?php
$service_url = "http://127.0.0.1:8000/api/thesis/?format=json";
//initialize a curl session
$curl = curl_init();
//set options for the transfer
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
curl_setopt($curl, CURLOPT_URL, $service_url);
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
//execute the session
$curl_response = curl_exec($curl);
//finish off the session
curl_close($curl);
$curl_jason = var_dump(json_decode($curl_response, true));
print_r($curl_jason);
echo $curl_jason;
?>
I think you should use GET method of curl like this
$service_url = "http://127.0.0.1:8000/api/thesis/?format=json";
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
//execute the session
$curl_response = curl_exec($curl);
//finish off the session
curl_close($curl);
$curl_jason = json_decode($curl_response, true);
print_r($curl_jason);
Use the below mentioned code snippet to fetch data from a REST API using PHP curl
<?php
function _isCurl(){
return function_exists('curl_version');
}
if (_iscurl()){
//curl is enabled
$url = "http://testDomainName/restAPI.php?id=123&amt=100&jsonp=?";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
print_r($output);
// Curl operations finished
}
else{
echo "CURL is disabled";
}
?>
I'm sending data using cURL on the following code block:
$content = $data['email'].'|'.$data['limit'].'|'.$data['docs'];
$curl = curl_init(ACCESS_URL);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$answer = curl_exec($curl);
curl_close($curl);
The webservice side do run and creates the file, but $_POST is empty. I just can't figure out why.
public function action_getAccess() {
file_put_contents('/home/donut/uploads/data.txt', $_POST);
}
From curl:
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/plain"));
And catch this as:
file_put_contents('/home/donut/uploads/data.txt', file_get_contents("php://input"));
I am trying to send a url from a php page to a json page in the format
"http://myserver.com/login?q={"myid":"phill","password":"mypass"}"
. If I paste this into a browser address it works correctly. I have tried HttpRequest and cURL with no success. Can you suggest how this may be achieved?.
<?php
$yourJSONEncodedData = array('userid' => "phill", 'password' => "mypassword");
$url = "http://myserver.com/login?q=".json_encode($yourJSONEncodedData);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
$output = curl_exec($curl);
curl_close($curl);
var_dump($output);
$response = json_decode($output,true);
echo $response;
?>
I am expecting "{}" to be returned to the php program.
I don't know why you are sending parameter in json format? You can send parameters in query string and get all the values in json page.
Make use of json_encode() in PHP.
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
The above example will output: {"a":1,"b":2,"c":3,"d":4,"e":5}
Posting the JSON data using cURL (Something like this)
$url="http://myserver.com/login?q=".$yourJSONEncodedData;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
$output = curl_exec($curl);
curl_close($curl);
var_dump($output);
That's all i can suggest without seeing your code.
I am new to web services. I have tried to request from the example web service at http://www.w3schools.com/webservices/tempconvert.asmx. This is the code I have tried:
<?php
$data = array('Celsius' => '56');
$curl = curl_init('http://www.w3schools.com/webservices/tempconvert.asmx
/CelsiusToFahrenheit');
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, 'http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);
curl_close($curl);
echo $result;
?>
I am not entirely sure, but should the data be a query string
$data = "Celsius=56";
I am sure you are doing this just to test curl. You don't really need a webservice to convert celsius to farenheit
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded"));
curl_setopt($curl, CURLOPT_POST,count($data));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
Use these attibutes. to fetch the data. as some url requires exact parameters.
Note* :- Headers are optional
I have a RESTful server at server.pad.com/authenticate (locally) that takes one parameter and returns JSON. So in Laravel its authenticate/(:any)
I'm trying to get data from an ajax request and send it to the server and send back the response. Heres what I've tried...
<?php
$json = json_decode($_POST['data'], true);
$url = 'http://service.pad.com/authenticate';
$curl = curl_init($url);
$data = json_encode($json);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl);
curl_close($curl);
echo json_encode($response);
?>
Probably Content-Type: application/x-www-form-urlencoded issue.
Your $data is in JSON. You set value with CURLOPT_POSTFIELDS, but not var.
As I can see, $_POST['data'] is in JSON.
In the case the server gets single data in JSON try this:
$url = 'http://service.pad.com/authenticate';
$curl = curl_init($url);
$postdata = array( 'data' => $_POST['data'] );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postdata) );
$response = curl_exec($curl);
curl_close($curl);
echo json_encode($response);
But if the server gets multiple vars, not JSON, try this:
$url = 'http://service.pad.com/authenticate';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS,
http_build_query( json_decode($_POST['data'], true) )
);
$response = curl_exec($curl);
curl_close($curl);
echo json_encode($response);