eway direct payment curl code not working - php

I am trying to charge the customer using eway payment method. I am implementing the procedure through curl and here is the code to the function.
public function testing_direct() {
$url = 'https://api.sandbox.ewaypayments.com/DirectPayment.json'; // PROD
$postData = array(
'Method' => 'TokenPayment',
'TransactionType' => 'Recurring',
'TokenCustomerID' => '918549937032',
'TotalAmount' => '1995',
'InvoiceNumber' => '123',
'InvoiceDescription' => 'testing'
);
$ch = curl_init($url);
$api_authentication = base64_encode('####;****');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json','Authorization:####'
),
CURLOPT_POSTFIELDS => json_encode($postData)
));
$response = curl_exec($ch);
// Check for errors
if ($response === FALSE) {
die(curl_error($ch));
}
$responseData = json_decode($response, TRUE);
print_r($responseData);
die;
}
This code doesnot print anything at all...Nothing in the error log either...any ideas that why it is not working??
Thanks

There are a few things that need to be fix to get that script working, particularly around the authentication which is the most immediate cause of a problem.
There needs to be a check of the HTTP status of the request since curl_exec only returns FALSE if there is an error - e.g. a 404 (page not found) or 401 (unauthorized) is still treated as a successful request.
// Check for errors
if ($response === FALSE) {
die(curl_error($ch));
}
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code != 200) {
die('HTTP error: '.$code);
}
The actual authentication appears to suffer from 2 main issues: there is a semicolon instead of a colon between the username (API key) and password, and the Basic part of the authentication is missing:
$api_authentication = base64_encode('####:****');
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json','Authorization: Basic '.$api_authentication
),
This could be simplified by just using CURLOPT_USERPWD:
CURLOPT_USERPWD => '####:****',
Apart from that, note that the $postData array needs some extra nesting: TokenCustomerID should be a child of Customer and TotalAmount and the Invoice fields should be children of Payment:
$postData = array(
'Method' => 'TokenPayment',
'TransactionType' => 'Recurring',
'Customer' => array(
'TokenCustomerID' => '911058757297',
),
'Payment' => array(
'TotalAmount' => '1900',
'InvoiceNumber' => '123',
'InvoiceDescription' => 'testing',
),
);
Check out the example in the documentation for a complete example request.
Good luck!

Related

getting no errors with curl and also dont get any response

am trying to login to netflix using php curl but i get no response at all or errors.here is my code
$handle = curl_init();
$url = "https://www.netflix.com/ke-en/login";
// Array with the fields names and values.
// The field names should match the field names in the form.
$postData = array(
'userLoginId' => 'Lady',
'password' => 'Gaga',
'submit' => 'ok'
);
curl_setopt_array($handle,
array(
CURLOPT_URL => $url,
// Enable the post response.
CURLOPT_POST => true,
// The data to transfer with the response.
CURLOPT_POSTFIELDS => $postData,
CURLOPT_RETURNTRANSFER => true,
)
);
$data = curl_exec($handle);
echo curl_error($handle);
echo curl_errno($handle);
curl_close($handle);
echo $data;
the error i get is 0 and i saw that it means it is successfull

LinkedIn REST API - Internal server error

I'm using the LinkedIn REST API to post updates to a users timeline.
Since a few days I get an Internal server error response from LinkedIn but the code worked before.
PHP:
$postTitle = "hello";
$postDesc = "world ";
$submitted-url = "http://example.com";
$submitted-image-url = "http://images.example.com/img/hello.jpg";
$comment = "foobar";
$postData = array('visibility' => array('code' => 'connections-only'),
'content' => array('title' => $postTitle,'description' => $postDesc,'submitted-url' => $postURL,'submitted-image-url' => $postImage), 'comment' => $postComment);
$ch = curl_init('https://api.linkedin.com/v1/people/~/shares?oauth2_access_token='.$oauthToken.'&format=json'
);
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array('x-li-format: json', "Content-Type: application/json"),
CURLOPT_POSTFIELDS => json_encode($postData)
));
$response = curl_exec($ch);
How to fix that error?
Your code is invalid PHP (perhaps because of some edits you made before posting?); modifying it to:
$postTitle = "hello";
$postDesc = "world ";
$postURL = "http://example.com";
$postImage = "http://images.example.com/img/hello.jpg";
$postComment = "foobar";
$oauthToken = "<token>";
$postData = array(
'visibility' => array('code' => 'connections-only'),
'content' => array(
'title' => $postTitle,
'description' => $postDesc,
'submitted-url' => $postURL,
'submitted-image-url' => $postImage
),
'comment' => $postComment
);
$ch = curl_init('https://api.linkedin.com/v1/people/~/shares?oauth2_access_token='.$oauthToken.'&format=json');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array('x-li-format: json', "Content-Type: application/json"),
CURLOPT_POSTFIELDS => json_encode($postData)
));
$response = curl_exec($ch);
works if only $oauthToken is set to a valid token. Assuming your real code is correct the only possiblity left is that your OAuth token has expired and you need to obtain a new one first. By adding CURLOPT_VERBOSE => TRUE to the cURL options you would find out more about the error that LinkedIn returns.
You may considering using the LinkedIn PHP SDK (provided by the community) instead: https://github.com/Happyr/LinkedIn-API-client
We faced similar issue with Linkedin API recently. Finally figured out the fix by changing the url.
New URL : "https://api.linkedin.com/v1/people/~/shares"
Instead of specifying 'oauth2_access_token' in the query string,
add it in the header - specify :
"Authorization", "Bearer " + accessToken.
And finally in the request body parameter, add your json/xml data to post
You have to Use your Authentication Token in Request Headers.
This is the working code. Try it.
$postTitle = "hello";
$postDesc = "world ";
$submitted-url = "http://example.com";
$submitted-image-url = "http://images.example.com/img/hello.jpg";
$comment = "foobar";
$oauthToken = "TokenHere";
$postData = array('visibility' => array('code' => 'connections-only'),
'content' => array('title' => $postTitle,'description' => $postDesc,'submitted-url' => $postURL,'submitted-image-url' => $postImage), 'comment' => $postComment);
$ch = curl_init('https://api.linkedin.com/v1/people/~/shares?format=json');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array('x-li-format: json', "Content-Type: application/json", "Bearer: ".$oauthToken.""),
CURLOPT_POSTFIELDS => json_encode($postData)
));
$response = curl_exec($ch);

LinkedIn API - posting to a group or profile share returns a 401 authentication code

I had a problem with no documented solution that I could find. Now that I found it, I'm posting it here in the event someone runs into the same issue.
I followed the steps to authenticate with LinkedIn and get an access token, I was able to retrieve my profile information and groups that I belong to without any issue.
Next, I wanted to make a post to a group using the API.
The LinkedIn API docs show the use of file_get_contents, but it was not working for me. The access token was correct, but I was receiving a 401 response. Refer to https://developer.linkedin.com/documents/code-samples. Because I added ignore_errors=1, the group post was made, but still returning a 401.
As reference, this was the piece of code that I had to change to resolve the 401:
$context = stream_context_create(
array('http' =>
array('method' =>"POST",
'header'=> "Content-Type:application/json\r\n",
'content' => $body,
'ignore_errors' => '1'
)
)
);
$res = file_get_contents($url, false, $context);
Solution Overview
Using the LinkedIn API to post to a group, the steps are:
Set up the URL:
$params = array('oauth2_access_token' => YOUR_ACCESS_TOKEN);
$url = 'https://api.linkedin.com/v1/groups/{group_id}/posts?' . http_build_query($params);
Set the body for the POST
$bodyArray = array(
'title' => $title,
'summary' => $userMessage,
'content' => array(
'title' => '$title2',
'submitted-image-url' => $pictureUrl,
'submitted-url' => $redirectUrl,
'description' => $userMessage
)
);
$body = json_encode($bodyArray);
Use CURL instead of get_file_contents This is what I needed to change to get it working.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => array('x-li-format: json', "Content-Type: application/json"),
CURLOPT_POSTFIELDS => $body,
));
// here we execute the code and check for response code
curl_exec($curl);
$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($http_status == "201"){
echo date('g:i') . ' Posted to LinkedIn group <br>';
}else{
echo date('g:i') . '<b>LinkedIn error: ' . $http_status . '</b><br>';
}

get data from database using curl and json

Do any of you know a way to get datas from a database of a website? I already studied and made a program that POST data using json. It gets the shipment status of a certain tracking number and if it's shipped, it will be changed to delivered. And I already done that.
Now what I'm trying to do is get all the datas, or the row of that tracking number and then encode it to JSON. But I don't know what to do.
This is how I did the first program: on getting a specific tracking number and update the status to delivered.
<?php
$url='https://sample.com.ph';
$apikey='apikey';
$method ='SendPackageStatus';
$data = array(
'package' => array(
'tracking_number' => '735898532',
'package_status' => 'delivered',
'failed_reason' => '',
'updated_at' => '2014-01-01 07:02:14'
)
);
$check=json_encode($data);
$postdata = "&data=$check&apikey=$apikey&method=$method";
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_URL => $url.'/webservice/',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_VERBOSE => true,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postdata,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => array('Content-type: application/x-www-form-urlencoded',
)
)
);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
Why not use mysql?... you can store json string on db (as..strings). I dont follow what you are trying to do.

updating an issue in JIRA using rest api and PHP in API 5.1

I am using the following code to update an issue in JIRA but unable to diagnose the error. The error I am getting is as follows:
HTTP Status 415 - Unsupported Media Type
type Status report
message Unsupported Media Type
The code I have written is as follows:
$resource_array['api_name'] = 'issue/SPC-60';
$resource_array['fields'] = array (
'summary' => 'CLONE - Testing label stuff',
'assignee' =>
array (
'emailAddress' => 'avinashk.dubey#gmail.com',
),
'customfield_10649' =>
array (
'id' => '10668',
),
'customfield_10616' => 'This is observation'
);
$data = putJiraAPI($resource_array);
print_r($data);
////////////////////////////////////////////
function putJiraAPI($resource_array)
{
$api_name = $resource_array['api_name'];
unset($resource_array['api_name']);
$result = put_to($api_name, $resource_array);
if(is_array($result))
{
return $result;
}
else
{
return "error while getting data using ".BASE_URL.API_URL.$resource_string;
}
}
function put_to($api_name, $resource_array)
{
$jdata = json_encode($resource_array);
print_r($jdata);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_URL => BASE_URL . API_URL . $api_name,
CURLOPT_USERPWD => USER_NAME . ':' . PASSWORD,
CURLOPT_POSTFIELDS => $jdata,
CURLOPT_HTTPHECURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
CURLOPT_RETURNTRANSFER => true
));
echo BASE_URL . API_URL . $api_name;
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result,true);
}
Actually, I did wrong here:
CURLOPT_HTTPHECURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
it should be:
CURLOPT_HTTPHEADER => array('Content-type: application/json'),

Categories