Trying to get API output in PHP - php

I am trying to get output of this API using php. It's a Australia Post Freight calculator. I am not sure what is wrong with it, can some one please suggest. It will be really helpful.
// Set your API key: remember to change this to your live API key in production
$apiKey = API_KEY;
// Set the URL for the Domestic Parcel Size service
$urlPrefix = URL_PREFIX;
$parcelTypesURL = 'https://' . $urlPrefix . '/postage/parcel/domestic/size.json';
// Lookup domestic parcel types (different kinds of standard boxes etc)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $parcelTypesURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('AUTH-KEY: ' . $apiKey));
$rawBody = curl_exec($ch);
// Check the response: if the body is empty then an error occurred
if(!$rawBody){
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}
// All good, lets parse the response into a JSON object
$parcelTypesJSON = json_decode($rawBody);

curl_init() is disabled for security reasons...
This means that the server has disabled that function.
If you have control of the server, then enable curl_init() in the php.ini.
More information here.
If you do not, try using file_get_contents(). more information here

Related

Pulling in optional parameters for Emma API using php

I am trying to get additional parameters from Emma's API using php. If anyone can point me in the right direction, I would greatly appreciate it. I know that my problem is terminology and thnk I am struggling with the basics and just need to "get over the hump."
I have been able to successfully test getting the basic fields that Emma provides by using get curl https://api.e2ma.net/account_id/mailings and --header 'Authorization: Basic {{public_api_key}}:{{private_api_key}}'
My problem is that according to their documentation (url below), I should be able to include extra parameters (I need to get the with_html_body parameter).
http://api.myemma.com/api/external/mailings.html
The code below gives me the following error:
Error getting member: {"error": "Unable to parse JSON request"}
I know the $data array is the problem.
Any idea if this is my only problem?
I figure I need to pull in the with_html_body field as an array and then CURLOPT_POSTFIELDS the variable ($data).
Amy I way off base?
Thanks in advance for any help pointing me in the right direction.
<?php
// Authentication Variables
$account_id = "1111111";
$public_api_key = "99999999";
$private_api_key = "99898989";
// Set URL
$url = "https://api.e2ma.net/" . $account_id . "/mailings";
// Open connection
$ch = curl_init();
$data = array('with_html_body' => 'true');
// Make the curl call
curl_setopt($ch, CURLOPT_USERPWD, $public_api_key . ":" . $private_api_key);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$head = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//curl_close($ch);
// check for errors
if($http_code > 200) {
print "Error getting member:\r\n";
print_r($head);
} else {
/*
print "Member information:\r\n";
print_r($head);
*/
$decoded = json_decode($head, true);
echo "<pre>";
print_r($head);
echo "</pre>";
/*
*/
}
curl_close($ch);
?>
Typical me, I over-complicated the issue!
All I needed to do was adjust the $url to below.
$url = "https://api.e2ma.net/" . $account_id . "/mailings?with_html_body=true";
Yes, I feel like an idiot!
But making progress.
Now to put this code in a better form to use on a project.

eBay API ReviseItem call using cURL returns Serious Error #5 and #10012

I'm developing some PHP to work with the eBay API with cURL and I have tried this:
<?php
$dev_id = "sandboxDevId"; //Changed for security reasons
$app_id = "sandboxAppId"; //Changed for security reasons
$cert_id = "sandboxCertId"; //Changed for security reasons
$site_id = "186"; //spain
$xmlVersion = "1.0";
$encoding = "utf-8";
$api_endpoint = "https://api.sandbox.ebay.com/ws/api.dll";
$auth_token = "theAuthToken"; //The Auth token (Long String) for the seller user.
//I have changed it for the real token
$itemid = "110178474743"; //The item id in sandbox
$new_description = "Updating my item description with cURL";
$new_description = urldecode($new_description);
$stringXML = "<?xml version=\"".$xmlVersion."\" encoding=\"".$encoding."\"?>";
$stringXML .="<ReviseItemRequest xmlns=\"urn:ebay:apis:eBLBaseComponents\">";
$stringXML .="<ErrorLanguage>en_US</ErrorLanguage><Item><ItemID>".$itemid."</ItemID>";
$stringXML .="<Description>".$new_description."</Description>";
$stringXML .="</Item><RequesterCredentials>";
$stringXML .="<eBayAuthToken>".$auth_token."</eBayAuthToken></RequesterCredentials>";
$stringXML .="<WarningLevel>High</WarningLevel></ReviseItemRequest>";
$feed = new SimpleXMLElement($stringXML);
$headers = array
(
'X-EBAY-API-COMPATIBILITY-LEVEL: 963',
'X-EBAY-API-DEV-NAME: ' . $dev_id,
'X-EBAY-API-APP-NAME: ' . $app_id,
'X-EBAY-API-CERT-NAME: '. $cert_id,
'X-EBAY-API-CALL-NAME: ReviseItem',
'X-EBAY-API-SITEID: ' . $site_id,
);
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, $api_endpoint);
curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
curl_setopt($connection, CURLOPT_POST, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, $feed);
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($connection);
curl_close($connection);
echo $response;
?>
I have tried to change the item description via the Developer tool and it worked, and I have printed the XML as plain text and it seems to be correct, but for some reason i get the following error:
2016-05-26 08:38:32 100121SeriousError00RequestError 51SeriousError00RequestError
It drops me two errors, i have checked them on the API reference:
#5 Serious error: XML Parse error.
and
#10012 Serious error: Invalid value for header "replaceable_value".
And i can't figure out how to solve that problems.
It will be helpful if someone can throw some light about this problem; It is the first time I work with cURL and XML and every help will be apreciated.
#10012 Serious error: Invalid value for header "replaceable_value".
I just had to inspect the element locally (F12 on browser) and it says that the error comes from the API detail level.
I added the following config line to the headers:
X-EBAY-API-DETAIL-LEVEL: 0;
and error #10012 seems to be solved.
Error #5 appeared because i was sending the XML to eBay servers the wrong way. I just had to pass the string to the cURL as a parameter:
curl_setopt($connection, CURLOPT_POSTFIELDS, $stringXML);
instead of trying to parse it into an XML object.

Does Marketo API block curl on a per account basis?

I am trying to connect to the Marketo.com REST API using curl.
I can't get a response from the identity service. I only get an error message
"[curl] 6: Couldn't resolve host 'MY_CLIENT_ENDPOINT.mktorest.com'
,
but I can print the constructed url and paste it into a browser address bar and this will provide the expected response with the access_token element.
I can use curl in php and in a terminal to access my gmail account so curl is able to access an https service.
I have tried sending the parameters in the curl url as a get request and also by declaring them with curl's -F option as a post request
My application uses dchesterton/marketo-rest-api available on github, but I have also tried a simple php curl request just to get the access token.
private function getToken() {
$url = "$this->client_url/identity/oauth/token?grant_type=client_credentials&client_id=$this->client_id&client_secret=$this->client_secret";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$errors = curl_error($ch);
curl_close($ch);
file_put_contents($this->logDir . 'access_token_response' . date('Y-m-d') . '.txt', $url . "\n" . $response . "\n", FILE_APPEND);
if ($errors) {
file_put_contents($this->logDir . 'access_token_errors' . date('Y-m-d') . '.txt', $errors . "\n", FILE_APPEND);
}
return $response['access_token'];
}
Again, this fails with the same error but produces a perfectly formed url that I can paste into the browser and get a valid response.
I have also tried this using post instead of get as I have for every other test mentioned, and these have been tried on my localhost and on a test server.
Can anyone explain to me why this would fail?
Does Marketo block curl on a per account basis?
I was trying to implement something similar but my code wasn't working. I'm not sure exactly what is failing but I tried your code and it seems to work perfectly after some slight modifications:
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($request_data));
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($curl);
$errors = curl_error($curl);
curl_close($curl);
I hope this helps.

foursquare Userless access curl php

I'm call the foursquare api with the userless access approach in php using curl.
The foursquare endpoint venues search is supposed to support userless access.
$city = urlencode($city);
$location= urlencode($location);
$url = 'https://api.foursquare.com/v2/venues/search?query=' . $location . ' &near=' . $city . '&v=20130815&client_id=xxx&client_secret=xxx';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);
The response:
[code] => 400
[errorType] => invalid_auth
[errorDetail] => Missing access credentials. See https://developer.foursquare.com/docs/oauth.html for details.
afaik, using the 'userless access' approach does not require oauth. I'm including the client id and secret.
So why am I getting an 'invalid_auth' error ?
if I enter the output of $url in a browser, I get a correct response. If I hard-code the $url, then curl returns a correct response. So why would the dynamic $url generate auth error ?
Solution:
' &near=' should be '&near=' -argh

PHP cURL - Security When Processing Credit Card Transaction

I am working with a credit card processor script -- provided by the processor -- which takes in various credit card data and merchant account data, fetches a cURL, and parses the return XML for transaction status.
Code:
define("CURL_PROCESSING_URL", "https://ideposit.vbprograms.net/servlet/pg");
$params = "Merchant_User_Name=" . "vitale" .
"&Merchant_Password=" . "test" .
"&Tracking_Number=" . "00001" .
"&Credit_Card_Number=" . "4012888888881" .
"&Credit_Card_Exp_Date=" . "1205" .
"&Charge_Amount=" . "12.00" .
"&AVS_Street=" . "8320" .
"&AVS_Zip_Code=" . "85284" .
"&CV_Security_Code=" . "999" .
"&Credit_Card_Type=" . "MC_CARD_VISA" .
"&CardHolder_Name=" . "test Card Holder";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, CURL_PROCESSING_URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$returned = curl_exec($ch);
curl_close($ch);
$p = xml_parser_create();
xml_parse_into_struct($p, $returned, $vals, $index);
xml_parser_free($p);
Questions:
1) Does SSL_VERIFYPEER being set to false make it easier for the server to be spoofed and transaction data to be intercepted?
2) Is the cURL URL and associated POST values stored in server logs or transferred in plain text?
3) Is there a safer way or set of options for carrying this transaction out?
I would think in a production environment, you would want to use default value of CURLOPT_SSL_VERIFYPEER = true. This verifies that the SSL cert is valid. For testing or develpoment environments where perhaps the processor gives you a sandbox to work with that may have a self-signed cert or a cert that is expired or similar, you would probably be OK having this as false.
You are using SSL, so the data will not be sent in plain text. Without knowing what sort of error logging you have in place it is hard to say whether it would be stored on server logs.
cURL is totally fine for doing what you are trying to do assuming that you always use SSL and you are not logging sensitive data.

Categories