I am new to PHP and have been trying to access to the API for the past 2 hours. Everytime i request for the json object, it kept giving me null. I am sure i filled up the URL and Accountkey correctly. Can comeone please correct me if i did anything wrong with the code below. Thank you.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://urlxxxxxxxx.com');
$header = array(
'AccountKey:' => 'xxxx',
'Accept:' => 'application/json');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
$obj = json_decode($result);
curl_close($ch);
print "<pre>";
print_r($obj);
print "</pre>";
?>
Try to set CURL http headers like this, not as php array key=>value pattern.
$header = array(
'AccountKey: xxxx',
'Accept:application/json'
);
Related
I want to fetch Api response (created in nodejs) in website using Php,So for this i am using
curl but its not working,I tried with following code but not working for me (showing blank page),Where i am wrong ? Here is my code
$post = ['email'=> "example#xyz.com",'password'=> "testing"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://35.154.149.228:8000/api/admin/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);
$result = json_decode($response);
print_R($result);
Change
$result = json_decode($response);
To
$result = json_decode($response, true);
Then
echo '<pre>';
print_r($result);
Response:-
Array
(
[statusCode] => 401
[error] => Unauthorized
[message] => Invalid username or password
[responseType] => INVALID_USER_PASS
)
change your post array
$post = array('email'=> "example#xyz.com",'password'=> "testing");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://35.154.149.228:8000/api/admin/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);
$result = json_decode($response);
print_r($result);
Try displaying errors just in case the errors/warnings are suppressed.
use these at the top of the file, just after the php tags
ini_set("display_errors", "On");
error_reporting(E_ALL);
Also try to print the raw response before json_decoding it, this is because if the response you are getting is not valid json nothing would be printed out after decoding it.
Use this
print_r("The response is: " . $response);
In summarry your code should look like
ini_set("display_errors", "On");
error_reporting(E_ALL);
$post = ['email'=> "example#xyz.com",'password'=> "testing"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://35.154.149.228:8000/api/admin/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);
//Printing the original response before trying to decode it
//$result = json_decode($response);
print_r("The response from the server before decoding is: " . $response);
Let us know what the exact response you get from this is
Hi there I never develop in php but I have to create a small function to do a post request to my meteor node application. The end point works fine I have been testing it. I am trying to use the post request to get my login token back. Providing username and password it should return json data even it is incorrect it returns json data saying login refused.
It seem noting is happening tho. My return data seems to always be null.
As I said I never really used php nore do I plan on using it much. But here is my code probably very easy mistake to fix.
<?php
$ch = curl_init();
$params = array(
"username" => "apilogin",
"password" => "12345"
);
echo httpPost("http://127.0.0.1:3000/api/login", $params);
function httpPost($url,$params)
{
$postData = http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
$output=curl_exec($ch);
// Check for errors
if($output === FALSE){
echo "false";
die(curl_error($ch));
}
// Decode the response
$output = json_decode($response, TRUE);
curl_close($ch);
return $output;
}
?>
change:
$output = json_decode($response, TRUE);
into:
$output = json_decode($output, TRUE);
you never made a variable $response
i am having trouble getting a JSON file to a php-array.
i got a json-file as response from an api (request done with curl)
and want to make an array out of it but it won't work.
Here is my code:
<?php
class modExpose{
public static function getFunction($id){
//In my code i am "preparing" the request here
// *********** cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url.$qry_str);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
}
$id = $_GET['id'];
$data = modExpose::getFunction($id);
$array = json_decode($data,true);
print_r($array);
?>
the print_r function only delivers: 1. (same does the var_dump() function).
I also tried adding html_entity_decode() but the problem still remains.
Thank's for helping!
That is probably because the return value of your curl_exec() call is true on success and that is all you are returning from your method.
If you want to get the data that was returned by the curl call, you need to set the CURLOPT_RETURNTRANSFER option:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url.$qry_str);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
// Return the result on success
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
// Now response will contain the results of your curl call
$response = curl_exec($ch);
Apart from that I assume you have checked the variables that seem to be undefined in your example code.
I'm trying to make an CURL PUT REQUEST, my code looks like:
public function assignProfile($token, $organizationId, $profileId)
{
//define enviroment and path
$host = enviroment;
$path = "/admin/organizations/".$organizationId."/users";
$data_string = '["'.$profileId.'"]';
// set up the curl resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host.$path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer '.$token.'',
'Content-Length: ' . strlen($data_string)
));
echo "<br>OK<br>";
// execute the request
$output = curl_exec($ch);
// return ID for a new case
$output = json_decode($output);
var_dump($output);
}
Each part looks correct, when I var_dump $path, $host, even $data_string looks correct. However var_dump() at the end throw just NULL
I expect I'm doing something wrong or missing something really important.
May I ask you for some advise?
Thanks
EDIT:
What i do with it:
// define
define("Audavin","here is some uniqe ID");
.
.
.
$Users = new Users;
// this return Auth token ( I verify this work with echo )
$token = $Users->authorization();
// Calling method mentioned above
$Users->assignProfile($token,"here is org id", Audavin);
I would start by making sure the URL you're making the request to actually works and returns a valid response, you can do so by using a simple REST client (like POSTMAN chrome extension for example)
If you do get a response back, try and see if it's indeed a valid JSON, if not, that could be why you're not getting anything back from json_decode (more on return values here: http://php.net/manual/en/function.json-decode.php)
Finally, It is suggested you add curl_close($ch) to the end of your code to make sure your release the curl handle.
i am making a POST request using curl and PHP, sending a username/password array as a JSON object.
so far all is working wonderfully. now i wanted to read the headers so i can parse the cookie. i know i can use a file/jar and have tested that - works well. i want to not write a thing to disk.
so i flagged CURLOPT_HEADER as TRUE and i can parse the cookie value.
HOWEVER - the returned data is gone. totally gone...
when i flag _HEADER FALSE - i see the response.
the code:
$login_array = array('login' => array('username' => $username, 'password' => $password));
$login_json = json_encode($login_array);
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE); <-- this line f***s it all up...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $login_json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_URL, 'http://localhost/test/token');
$result = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code != 200) { /* do something */ }
preg_match_all('|Set-Cookie: (.*);|U', $result, $content);
$cookies = implode(';', $content[1]);
echo "cookie: $cookies<br>"; <-- thanks _HEADER for the cookie value
$response = json_decode($result);
$user_id = $response->user->id;
echo 'user_id: ' . $user_id . '<br>'; <-- empty man... headers are on means body is off...
i've been digging through different posts and it seems the order of the opts makes a difference. tried moving them about - no luck.
thoughts?!
Use:
$body=mb_substr($result, curl_getinfo($ch,CURLINFO_HEADER_SIZE));
$response = json_decode($body);
$user_id = $response->user->id;
echo 'user_id: ' . $user_id . '<br>';
try this
curl_setopt($ch,CURLOPT_NOBODY,false)