The main function is that one but I don't get any values, it works fine in a 000webhost domain but with a VPS I don't recieve any values.
(https://i.stack.imgur.com/uPVMV.png)
I have already installed php-curl and php-json, Am I missing some dependency?
Php Code
<?php
//Building A Simple Bittrex Bot
$apikey='2923c158d5754c29a088c4adea5c6f34';
$apisecret='3f29b39ab1be46afb0834fe56c8e4fea';
function bittrexbalance($apikey, $apisecret){
$nonce=time();
$uri='https://bittrex.com/api/v1.1/account/getbalance?apikey='.$apikey.'¤cy=BTC&nonce='.$nonce;
$sign=hash_hmac('sha512',$uri,$apisecret);
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$execResult = curl_exec($ch);
$obj = json_decode($execResult, true);
$balance = $obj["result"]["Available"];
return $balance;
}
?>
Here is the link to code on the github.
The problem is obviously here:
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
The documentation of curl_exec() clearly states:
Return Values
Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.
Add a call to curl_setopt() passing CURLOPT_RETURNTRANSFER to let curl_exec() know you want it to return and not to display the body of the HTTP response.
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$execResult = curl_exec($ch);
Related
Description
I'm trying to make a request to an API via PHP cURL
$access_token = $tokens['access_token'];
$headers = array(
"Authorization: Bearer " . $access_token
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,env('USER_INFO'));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1000);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_USERAGENT,'php');
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
$info = curl_getinfo($ch);
$userInfo = curl_exec($ch);
$userInfo = json_decode($userInfo, true);
dd($userInfo);
I kept getting this back
{"sub":"acr:123;type=STAT","updated_at":1509463516,"name":"User","email":"user#email.com"}
1
Try #2
If I do
$userInfo = json_encode($userInfo, true);
I got
{"sub":"acr:123;type=STAT","updated_at":1509463516,"name":"User","email":"user#email.com"}
"true"
How do I get rid of the 1 or true below it, and only get the JSON data?
Is there another param for json_decode() that I need to pass in?
Set the CURLOPT_RETURNTRANSFER curl option. curl_exec is just returning true without that, (it echoes the response and returns true; see curl_exec return values) and json_decode(true) is true.
How would one go about and debug this further?
A couple of things to experiment with:
Remove $userInfo = json_decode($userInfo, true); and
dd($userInfo);. Without the CURLOPT_RETURNTRANSFER option set, you should still see the non-decoded JSON, but not the true or 1.
Add another dd($userInfo); before the json_decode. You'll see what curl_exec actually returned, which may help you eliminate json_decode as a possible cause for this odd looking behavior.
The following line should solve your problem with the response.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
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 am trying to access the Toggl Reporting API.
I tried following in PHP with cURL, which connects to the API but gives the following error message: 'This method may not be used.' Any light on why this is the case would be useful as I'm very new to webservices. I may be missing something obvious or totally going the wrong way about it, so apologies if this is the case.
<?php
$userAgent = 'xxx';//username
$token = 'xxx';//token
$returned_content = get_data('https://toggl.com/reports/api/v2/summary?&workspace_id=[workspaceid]&since=2013-05-19&until=2013-05-20&user_agent=[username here]');
print_r($returned_content);
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_USERPWD, $token.':api_token');
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
Edit: I tried a different approach. If I run the following code, I no longer receive any error messages, so the code seems to be executing but I can't print the response to the screen. Is there something specific I need to do to view the output other than print_r?(Toggl API returns JSON). Thanks.
$json = curl%20-v%20-u%[myapitoken]:api_token%20https://toggl.com/reports/api/v2/weekly?workspace_id=[id]&wsid=282507&since=2012-08-19&until=2013-09-20&user_agent=[user].json;
print_r($json);
Edit: Finally resolved! Code is as follows:
$workspace_id = '[id here]';
$user_agent = '[user agent here]'; // no spaces
$api_token = '[token here]';
$report_url = 'https://toggl.com/reports/api/v2/weekly?user_agent='.$user_agent.'&since=2013-08-01&until=2013-09-01';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_USERPWD, $api_token . ':api_token');
curl_setopt($ch, CURLOPT_URL, $report_url . '&workspace_id=' . $workspace_id);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($ch);
$result = json_encode($result);
Hope this helps someone in the future!
As I understand, you are receiving this message because of CURLOPT_SSL_VERIFYPEER == FALSE.
Try to remove this string from the code:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
Maybe I wrong, but I think with this option you are receiving "HTTP 501 Not Implemented" error from the Toggl server, which contains exactly the same message, "This method may not be used."
I can call a soap server using java like this
Call call = new Call();
URL url = new URL("http://soap-something.dash.com/servlet/rpcrouter");
call.setTargetObjectURI("urn:login-transport");
call.setMethodName("confirmPassword");
call.setParams(a vector);
resp = call.invoke(url, "");
But my question is how can I call this same function using curl and php, I have already tried this, but it may be some kind of funny code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://soap-something.dash.com/servlet/rpcrouter?urn:login-transport");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, "confirmPassword");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("username"=>"pritom", "password"=>"pritom"));
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "<br/>HTTP CODE: " . $httpCode;
print_r($head);
But it echo http code 100 and I do not found any result from soap server. But my soap server is ok, tested by java.
Looks fine to me, except CURLOPT_HEADER needs to be either true or false (include HTTP header in output or not),
curl_setopt($ch, CURLOPT_HEADER, "confirmPassword");
should be
curl_setopt($ch, CURLOPT_HEADER, true);
I can only guess what confirmPassword is, if it's a callback, you should use CURLOPT_HEADERFUNCTION instead.
But as Ariel pointed out, you're not telling us what the problem is.
Try removing this line:
curl_setopt($ch, CURLOPT_NOBODY, false); // remove body
Then post what the output is.