First request to API:
$siteUrl = $this->getConfig()->get('siteUrl');
$headers = array(
"Espo-Authorization: " . $this->getEncodedLoginData()
);
$curl = curl_init($siteUrl . '/api/v1/Account');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
if (!$response) {
$GLOBALS['log']->addWarning('curlerr: ' . curl_error($curl));
}
curl_close($curl);
$response = json_decode($response, true);
I get accounts info as expected. But when i try to do next request after some manipulations with accounts, in response i get empty string.
There is an example of next request:
$siteUrl = $this->getConfig()->get('siteUrl');
$headers = array(
"Espo-Authorization: " . $this->getEncodedLoginData()
);
$curl = curl_init($siteUrl . '/api/v1/Contacts');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
if (!$response) {
$GLOBALS['log']->addWarning('curlerr: ' . curl_error($curl));
}
curl_close($curl);
Doing requests to API from browser works properly.
Problem were in login/pass data. I don't know exactly how, but login/pass vars were changed before executing second request. Thanks for assistance!
Related
My database is only storing the push tokens for user devices, not the HWIDs.
Is it possible to use the Pushwoosh API to deregister a device with the push token only? It's not working when I send just the push token with an empty string for the hwid.
Here's my code:
$device_id = '';
$pushwoosh_push_token = 'Push-Token-here';
$pushwoosh_app_code = 'App-Code-here';
$pushwoosh_auth_token = 'Auth-Token-here';
$response = unregister_device_pushwoosh($device_id, $pushwoosh_push_token, $pushwoosh_app_code, $pushwoosh_auth_token);
print_r($response)."<br>";
function unregister_device_pushwoosh($device_id, $pushwoosh_push_token, $pushwoosh_app_code, $pushwoosh_auth_token) {
$url = 'https://cp.pushwoosh.com/json/1.3/unregisterDevice';
$data = array(
'request' => array(
'application' => $pushwoosh_app_code,
'push_token' => $pushwoosh_push_token,
'hwid' => $device_id,
),
);
$content = json_encode($data);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
"Content-type: application/json\r\n".
"Authorization: Basic ".base64_encode("$pushwoosh_app_code:$pushwoosh_auth_token")
]);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if (200 != $status)
{
echo("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ', curl_errno ' . curl_errno($curl) . '<br>');
}
curl_close($curl);
return 'Response: ' . $json_response . "\n";
}
I'm trying to learn the shopify api for a project. Tried a few pasted codes on their forum, adapting my code to theirs.
The documentation says to do the authentication this way, by providing the following:
https://{username}:{password}#{shop}.myshopify.com/admin/api/{api-version}/{resource}.json
{username} — The API key that you generated
{password} — The API password
{shop} - The name that you entered for your development store
{api-version} — The supported API version you want to use
{resource} — A resource endpoint from the REST admin API
I'm trying to do a GET request on all the orders made on the store.
/ info
$API_KEY = '75c89bf******ea919ce****7702';
$PASSWORD = '2061******82d2802**f***9403';
$STORE_URL = '*****-na-***-c***.myshopify.com';
$AUTHORIZATION = base64_encode($API_KEY . ':' . $PASSWORD);
$url = 'https://' . $API_KEY . ':' . $PASSWORD . '#' . $STORE_URL . '/admin/api/2020-01/orders.json';
$header = array();
$header[] = 'Accept: application/json';
$header[] = 'Content-Type: application/json';
$header[] = 'Authorization: Basic ' . $AUTHORIZATION;
$session = curl_init();
//options
curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_HTTPHEADER, $header);
curl_setopt($session, CURLOPT_GET, 1);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
//exec
$response = curl_exec($session);
curl_close($session);
print_r($response);
// error
if($response === false)
{
print_r('Curl error: ');
}
The code doesn't work at all, without any error code, completly blank, with only the first project echo showing.
I verified my API keys and they are working, I can insert them manually into chrome.
You don't need the header for authorization. Your code should like:
$API_KEY = '75c89bf******ea919ce****7702';
$PASSWORD = '2061******82d2802ff***9403';
$STORE_URL = 'porcao-na-sua-casa.myshopify.com';
$url = 'https://' . $API_KEY . ':' . $PASSWORD . '#' . $STORE_URL . '/admin/api/2020-01/orders.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE) //can check status code, requst successfully processed if return 200
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
Found the problem when I ran in terminal, did not install php-curl on this machine.
#Kshitij solution worked, just like mine, when curl was properly installed.
// Provide the required parameters like store url , endpoint etc.
function shopifyApiCall($STORE_URL ,$query = [], $endpoint, $API_KEY, $PASSWORD){
//API_Key : your API key, you can get it from your APP setup.
//Password : Your Access Token
$url = 'https://' . $API_KEY . ':' . $PASSWORD . '#' . $STORE_URL . $endpoint;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt ($ch, CURLOPT_POSTFIELDS, json_encode($query));
$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$error_number = curl_errno($ch);
$error_message = curl_error($ch);
curl_close($ch);
// Return an error is cURL has a problem
if ($error_number) {
return $error_message;
} else {
// No error, return Shopify's response by parsing out the body and the headers
$response = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
// Convert headers into an array
$headers = array();
$header_data = explode("\n",$response[0]);
$headers['status'] = $header_data[0]; // Does not contain a key, have to explicitly set
array_shift($header_data); // Remove status, we've already set it above
foreach($header_data as $part) {
$h = explode(":", $part);
$headers[trim($h[0])] = trim($h[1]);
}
// Return headers and Shopify's response
//return array('headers' => $headers, 'response' => $response[1]);changed headers
return array('headers' => $header_data, 'response' => $response[1]);
}
}
I am running into an issue trying to issue a POST to the chargify API log payments from within a form.
However, it appears that the POST is actually being sent as a GET for some reason as it's returning an error.
Can anyone offer any advice on what I may be doing wrong? Postman is working just fine sending a POST with the JSON as the body, but I can't get it to work from PHP.
(note: the <urlmaskedforprivacy> is obviously not in my actual code)
//find invoice id # using invoice number search form
$invoiceNumber = fRequest::get('invoicenumber');
$json = file_get_contents('https://<maskedforprivacy>.chargify.com/invoices.json?number=' . $invoiceNumber);
$returnedInvData = fJSON::decode($json);
$invoiceId = $returnedInvData[0]->invoice->id;
// grab form data as payment info to send to chargify
$paymentAmount = fRequest::Get('amount');
$memo = fRequest::Get('memo');
if ((isset($invoiceNumber,$paymentAmount,$memo))) {
$url = 'https://<maskedforprivacy>.chargify.com/subscriptions/' . $invoiceId . '/payments.json';
$params = array(
'payment' => array(
'amount' => $paymentAmount,
'memo' => (string)$memo
),
);
// encode as json
$content = FJSON::encode($params);
// send to chargify
$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);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);
}
I've used this one code, but it returns 401 error and asks for Basic auth. in json_response variable. May be you've missed it?
$paymentAmount = 10;
$memo = 'test';
$url = 'https://test.chargify.com/subscriptions/1/payments.json';
$params = array(
'payment' => array(
'amount' => $paymentAmount,
'memo' => (string)$memo
),
);
// encode as json
$content = json_encode($params);
// send to chargify
$curl = curl_init($url);
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);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);
I have a sand box account in fortnox and i am trying to get the access token using following code, but i keep getting same error:
$requestMethod = "GET";
$ch = curl_init();
$options = array(
'Authorization-Code: '. AUTHORIZATION_CODE .'',
'Client-Secret: '. CLIENT_SECRET .'',
'Content-Type: '. CONTENT_TYPE .'',
'Accept: '. ACCEPTS .''
);
curl_setopt ($ch, CURLOPT_CAINFO, "/xampp/htdocs/cacert.pem");
curl_setopt($ch, CURLOPT_URL, "https://api.fortnox.se/3/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, $options);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $requestMethod);
$curlResponse = curl_exec($ch);
$info = curl_getinfo($ch);
//echo 'Took ' . $info['total_time'] . ' seconds for url ' . $info['url'];
echo $curlResponse;
if ($curlResponse === FALSE) {
echo "cURL Error: " . curl_error($ch);
}
curl_close($ch);
Error: PHP / 4.5.37 Can not sign in , access tokens , or client- secret missing ( 2000311 ) .
I get the Access-Token using the following code which is very close to your code. It worked fine for me.
Your error is described on the Errors page, but it adds nothing new to the text you already shown.
Please note that you can retrive the Access-Token only once, check Authentication section in the Fortnox documentation.
An Access-Token can only be retrieved once with every Authorization-Code, multiple requests with the same Authorization-Code will make both the Authorization-Code and the Access-Token invalid.
public function actionRetrieveAccessToken($authCode, $clientSecret)
{
$headers = [
'Authorization-Code: ' . $authCode,
'Client-Secret: ' . $clientSecret,
'Content-Type: application/json',
'Accept: application/json',
];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api.fortnox.se/3/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$responseText = curl_exec($curl);
$response = json_decode($responseText, true);
if (isset($response['ErrorInformation'])) {
echo 'Request failed with error messages:' . PHP_EOL;
echo $response['ErrorInformation']['Error'] . ': ';
echo $response['ErrorInformation']['Message'];
echo ' (' . $response['ErrorInformation']['Code'] . ')' . PHP_EOL;
} else {
echo 'Access Token: ' . $response['Authorization']['AccessToken'] . PHP_EOL;
}
}
$content_type = 'application/json';
$api_url="api url here";
$curl = curl_init();
$headers = array(
'Content-Type: '.$content_type.'',
'Authorization : '.'Bearer ' . $this->access_token
);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
if (!empty($data))
{
$data = json_encode($data);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
$response = curl_exec($curl);
logthis($response,"utils");
curl_close($curl);
I'm trying to pull information from Evergage, but based on my console log debugging, the function terminates at the curl_exec function (see comment). debug_to_console("success"); and curl_error both return nothing at all. However, if I debug_to_console("success"); just before curl_exec, it returns properly. When I test run the function, it runs for about 30 seconds and then ends. I originally thought it was a timeout, so I tried removing any time restrictions but it still doesn't seem to be working. Any help would be greatly appreciated. Thanks!
function RetrieveData($account, $dataset, $kind, $apiToken)
{
$port = "";
if ($account === "localtest") {
$port = ":8443";
}
$requestURI = "https://" . $account . ".evergage.com" . $port . "/api/dataset/" . $dataset . "/" . $kind . ".json?_at=" . $apiToken;
debug_to_console("request URL: " . $requestURI);
$session = curl_init();
curl_setopt($session, CURLOPT_FAILONERROR, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($session, CURLOPT_HEADER, true);
curl_setopt($session, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
);
curl_setopt($session, CURLOPT_HTTPHEADER, $headers);
curl_setopt ($session, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($session, CURLOPT_URL, $requestURI);
// EVERYTHING RUNS FINE UP TO THIS POINT
set_time_limit(0);
$response = curl_exec($session);
debug_to_console("success");
echo 'Curl error: ' . curl_error($session);
$info = curl_getinfo($session);
$responseCode = $info["http_code"];
if ($responseCode >= 300) {
print("Error loading data: " . $responseCode . "<br/>");
print($response);
} else {
$body = substr($response, $info['header_size']);
$decoded_result = json_decode($body, true);
}
curl_close($session);
return $decoded_result;
}
Edit: I should mention that I have printed out $requestURI and it is correct. Navigating to it results in the JSON information that I need downloading right away.