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);
Related
When I post the data using ajax and send to post.php by curl then $_FILES variable is empty and get the data in $_POST variable. When I print $_POST var getting the following data on post.php.
[temp_upload_file] => #/tmp/php6OHgQc;filename=Penguins.jpg;type=image/jpeg
when I print "$_FILES" var getting empty data on post.php
Array
(
)
Code:
$url = "post.php";
$ch = curl_init($url);
// send a file
curl_setopt($ch, CURLOPT_POST, true);
$data = array('temp_upload_file' =>'#'.$_FILES['uploadfile']['tmp_name'].';filename='.$_FILES['uploadfile']['name'].';type='. $_FILES['uploadfile']['type']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// output the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
echo curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
What I am doing wrong here??
Thank you in advance
For php5.5+ there is a new curl_file_create() function you need to use
the # format is now deprecated.
Try using following code:
$url = "post.php";
$tmpfile = $_FILES['temp_upload_file']['tmp_name'];
$filename = basename($_FILES['temp_upload_file']['name']);
$ch = curl_init($url);
// send a file
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'uploaded_file' => curl_file_create($tmpfile, $_FILES['uploadfile']['type'], $filename)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// output the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
echo curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
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 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
I'm using Drupal 7 and the services module and I'm trying to update a user profile using PHP & Curl.
Do I always have to login before sending a "PUT/update" ?
This is my code so far :
<?php
// REST Server URL
$request_url = 'http://mywebsite/end/user/login';
// User data
$user_data = array(
'username' => 'user2',
'password' => 'pass1',
);
// cURL
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $request_url);
curl_setopt($curl, CURLOPT_POST, 1); // Do a regular HTTP POST
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($user_data)); // Set POST data
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($curl);
print $response;
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Check if login was successful
if ($http_code == 200) {
// Convert json response as array
$logged_user = json_decode($response);
}
else {
// Get error msg
$http_message = curl_error($curl);
die($http_message);
}
print_r($logged_user);
// REST Server URL
$request_url = 'http://mywebsite.com/end/user/8&XDEBUG_SESSION_START=netbeans-xdebug';
$user_data = array('current_pass' => 'pass1', 'pass' => 'pass2');
// Define cookie session
$cookie_session = $logged_user->session_name . '=' . $logged_user->sessid;
// cURL
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json',
'Content-type: application/json')); // Accept JSON response
curl_setopt($curl, CURLOPT_PUT, TRUE);
curl_setopt($curl, CURLOPT_HEADER, TRUE); // FALSE); // Ask to not return Header
curl_setopt($curl, CURLOPT_COOKIE, "$cookie_session"); // use the previously saved session
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
// Emulate file.
$serialize_args = json_encode($user_data);
$putData = fopen('php://temp', 'rw+');
fwrite($putData, $serialize_args);
fseek($putData, 0);
curl_setopt($curl, CURLOPT_INFILE, $putData);
curl_setopt($curl, CURLOPT_INFILESIZE, drupal_strlen($serialize_args));
$response = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Check if login was successful
$ret;
if ($http_code == 200) {
// Convert json response as array
$ret = json_decode($response);
}
else {
// Get error msg
$http_message = curl_error($curl);
die($http_message);
}
print_r($ret);
curl_close($curl);
}
?>
What am I missing here?
Nothing happens to my profile.
Any answer is welcomed!
Hope this can help u.
$service_url = 'http://mywebsite/end/user/login'; // .xml asks for xml data in response
$post_data = array(
'username' => 'user2',
'password' => 'pass1',
);
$post_data = http_build_query($post_data, '', '&'); // Format post data as application/x-www-form-urlencoded
// set up the request
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // have curl_exec return a string
curl_setopt($curl, CURLOPT_POST, true); // do a POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); // POST this data
// make the request
curl_setopt($curl, CURLOPT_VERBOSE, true); // output to command line
$response = curl_exec($curl);
curl_close($curl);
// parse the response
$xml = new SimpleXMLElement($response);
$session_cookie = $xml->session_name .'='. $xml->sessid;
if(empty($xml->session_name) && empty($xml->sessid)){
echo 'Wrong';exit;
}
$service_url = 'http://mywebsite/end/user/token'; // .xml asks for xml data in response
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_POST, true); // do a POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); // POST this data
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // have curl_exec return a string
curl_setopt($curl, CURLOPT_COOKIE, "$session_cookie"); // use the previously saved session
// make the request
curl_setopt($curl, CURLOPT_VERBOSE, true); // output to command line
$csrf_token = curl_exec($curl);
curl_close($curl);
$xml = new SimpleXMLElement($csrf_token);
$csrf_token = $xml->token;
$csrf_header = 'X-CSRF-Token: ' . $csrf_token;
// REST Server URL
$request_url = 'http://mywebsite/end/user/8&XDEBUG_SESSION_START=netbeans-xdebug';
$user_data = array('current_pass' => 'pass1', 'pass' => 'testing');
// cURL
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json',
'Content-type: application/json',$csrf_header)); // Accept JSON response
curl_setopt($curl, CURLOPT_PUT, TRUE);
//curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($user_data)); // Set POST data
curl_setopt($curl, CURLOPT_HEADER, TRUE); // FALSE); // Ask to not return Header
curl_setopt($curl, CURLOPT_COOKIE, "$session_cookie"); // use the previously saved session
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
// Emulate file.
$serialize_args = json_encode($user_data);
$putData = fopen('php://temp', 'rw+');
fwrite($putData, $serialize_args);
fseek($putData, 0);
curl_setopt($curl, CURLOPT_INFILE, $putData);
curl_setopt($curl, CURLOPT_INFILESIZE, strlen($serialize_args));
$response = curl_exec($curl);
curl_close($curl);
You can achieve this by using "CURLOPT_COOKIEJAR" for writing and preserving cookies but you also need to set "CURLOPT_COOKIEFILE" for reading. More info can be found at http://php.net/manual/en/function.curl-setopt.php
define('COOKIE_FILE', "/tmp/sess" . time() . $user_data['username']);
curl_setopt ($curl, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt ($curl, CURLOPT_COOKIEFILE, COOKIE_FILE);