I'm making a Drupal/PHP Module to upload information to Taleo (Talent Management) database using SOAP. This works well with regular data like text and dates, but not with a file.
The manual shows an example of a file attachment:
createAttachment Test Case:
<soapenv:Header/>
<soapenv:Body>
<urn:createAttachment>
<in0>webapi-5616904436472928038</in0>
<in1>15</in1>
<in2>test1.docx</in2>
<in3>test1.docx</in3>
<in4>application/vnd.openxmlformatsofficedocument.
wordprocessingml.document</in4>
<in5>
<!--type: base64Binary-->
<array>JVBERi0xLjQNJeLjz9MNCjYgMCBvYmogPDwvTGluZWFyaX==</array>
</in5>
</urn:createAttachment>
</soapenv:Body>
</soapenv:Envelope>
So I made a PHP file like this:
// Send attachment
$fileName = drupal_get_path('module', 'taleo') . '/test.txt';
$rawFile = fread(fopen($fileName, "r"), filesize($fileName));
$B64File = base64_encode($rawFile);
$params = array(
'in0' => $session,
'in1' => $candidate_id,
'in2' => 'test.txt',
'in3' => 'test.txt',
'in4' => 'text/plain',
'in5' => $B64File
);
$client_taleo->__call('createAttachment', $params);
When I do "echo $B64File" I get this: RmlsZSB1cGxvYWQgd2l0aCBEcnVwYWwgIQ==, so the file is being read correct.
But I always get this error:
ERROR: soapenv:Server.generalException-attBinDataArr is null.
Any ideas?
You forgot to encapsulate the base64-data in array-tags.
<array>JVBERi0xLjQNJeLjz9MNCjYgMCBvYmogPDwvTGluZWFyaX==</array>
Something like this should work:
$params = array(
'in0' => $session,
'in1' => $candidate_id,
'in2' => 'test.txt',
'in3' => 'test.txt',
'in4' => 'text/plain',
'in5' => array('array' => $B64File)
);
It was clear I had to do something with the array-tag, that's for sure.
The answer above deserves an "upvote", so I gave it one. But I found the correct answer myself... After a few seconds of "logic" thinking. :)
'in5' => array('array' => $B64File)
Related
I am trying to submit a post with JSON content. I always get this message back:
"Client
error: POST
https://sandbox-api-ca.metrc.com//strains/v1/create?licenseNumber=CML17-0000001
resulted in a 400 Bad Request response: {"Message":"No data was
submitted."}"
(All keys and license number are sandbox. I changed keys slightly so auth wont work. )
here is my code
public function metrc()
{
$client = new Client();
$url = 'https://sandbox-api-ca.metrc.com//strains/v1/create?licenseNumber=CML17-0000001';
$request = $client->post($url, [
'headers' => ['Content-Type' => 'application/json'],
'json' => ['name' => "Spring Hill Kush"],
'auth' => ['kH-qsC1oJPzQnyWMrXjw0EQh812jHOX52ALfUIm-dyE3Wy0h', 'fusVbe4Yv6W1DGNuxKNhByXU6RO6jSUPcbRCoRDD98VNXc4D'],
]);
}
Your code is correct, it should works as expected. Seems that the issue is on the server side. Maybe the format of the POST request is not correct?
BTW, 'headers' => ['Content-Type' => 'application/json'] is unnecessary, Guzzle sets the header by itself automatically when you use json option.
My company uses has a lot of internal APIs that use very specific header and formatting requirements. I am new to SOAP::Lite and I'm trying to make it work within the company's framework.
Try #1:
Ideally, I would like to be able to just take the raw XML template (see bottom of the post), populate some placeholder variables, and send it to the endpoint using the following code:
my $client = SOAP::Lite->new( proxy => "$serviceURL");
my $reply = $client->InquireEnterpriseOrderDataRequest($rawxml);
However, this results in my header and request sections being enclosed in it's own "envelope", "body" and "InquireEnterpriseOrderDataRequest" which is rejected by the service.
Try #2:
The next thing I tried was to break my request into two pieces: header and request and use SOAP::Data and SOAP::Header to send those:
my $rawxmlheader = '<ns2:MessageHeader xmlns:ns2="http://mycompany.com/MessageHeader.xsd" xmlns="http://mycompany.com/CingularDataModel.xsd">
<ns2:TrackingMessageHeader>
<version>111</version>
<originalVersion/>
<messageId/>
<originatorId>ABC</originatorId>
<responseTo/>
<returnURL/>
<timeToLive>360000</timeToLive>
<conversationId>9AF0E9281A524262980F5284F4C57888_CCE423E277C74FA9A84D2155CD612EB3_0</conversationId>
<routingRegionOverride/>
<dateTimeStamp>2017-05-12T12:47:53Z</dateTimeStamp>
<uniqueTransactionId>mytransid</uniqueTransactionId>
</ns2:TrackingMessageHeader>
<ns2:SecurityMessageHeader>
<userName>myusername</userName>
<userPassword>mypass</userPassword>
</ns2:SecurityMessageHeader>
<ns2:SequenceMessageHeader>
<sequenceNumber/>
<totalInSequence/>
</ns2:SequenceMessageHeader>
</ns2:MessageHeader>';
my $rawxmlrequest = '<OrderSearchCriteria>
<OrderDetails>
<SearchByOrderAction>
<orderActionNumber>12345654</orderActionNumber>
<orderActionVersion>1</orderActionVersion>
</SearchByOrderAction>
</OrderDetails>
</OrderSearchCriteria>
<provisioningDetailsIndicator>true</provisioningDetailsIndicator>';
my $client = SOAP::Lite->new( proxy => "$serviceURL");
my $header = SOAP::Header->type('xml' => $rawxmlheader);
my $elem = SOAP::Data->type('xml' => $rawxmlrequest);
my #arguments;
push(#arguments, $header);
push(#arguments, $elem);
my $reply = $client->InquireEnterpriseOrderDataRequest(#arguments);
This produced a very similar request to what was needed with the exception that the InquireEnterpriseOrderDataRequest blob did not contain the xsi:schemaLocation, xmlns or xmlns:xsi values that seem to be required.
Try #3:
Now I was grasping at straws, so I also tried to granularly create my own XML using something like this:
my $temp_elements =
SOAP::Data->name("OrderSearchCriteria" => \SOAP::Data->value(
SOAP::Data->name("OrderDetails" => \SOAP::Data->value(
SOAP::Data->name("SearchByOrderAction" => \SOAP::Data->value(
SOAP::Data->name("orderActionNumber" => '301496944'),
SOAP::Data->name("orderActionVersion" => '3')
)
)
)
))
)->type("SomeObject");
my $client = SOAP::Lite->new( proxy => "$serviceURL");
my $reply = $client->InquireEnterpriseOrderDataRequest($temp_elements);
The problem here was that I don't know how to include the xsi:schemaLocation, xmlns and xmlns:xsi values or prepend the header information.
Of course, I'd like to go with the simplest possible implementation but any suggestions are appreciated! Thanks in advance!
Required request format:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
<ns2:MessageHeader xmlns:ns2="http://mycompany.com/MessageHeader.xsd" xmlns="http://mycompany.com/CingularDataModel.xsd">
<ns2:TrackingMessageHeader>
<version>111</version>
<originalVersion/>
<messageId/>
<originatorId>ABC</originatorId>
<responseTo/>
<returnURL/>
<timeToLive>360000</timeToLive>
<conversationId>9AF0E9281A524262980F5284F4C57888_CCE423E277C74FA9A84D2155CD612EB3_0</conversationId>
<routingRegionOverride/>
<dateTimeStamp>2017-04-11T18:47:53Z</dateTimeStamp>
<uniqueTransactionId>mytransid</uniqueTransactionId>
</ns2:TrackingMessageHeader>
<ns2:SecurityMessageHeader>
<userName>myusername</userName>
<userPassword>mypass</userPassword>
</ns2:SecurityMessageHeader>
<ns2:SequenceMessageHeader>
<sequenceNumber/>
<totalInSequence/>
</ns2:SequenceMessageHeader>
</ns2:MessageHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<InquireEnterpriseOrderDataRequest xsi:schemaLocation="http://mycompany.com/InquireEnterpriseOrderDataRequest.xsd" xmlns="http://mycompany.com/InquireEnterpriseOrderDataRequest.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<OrderSearchCriteria>
<OrderDetails>
<SearchByOrderAction>
<orderActionNumber>12345654</orderActionNumber>
<orderActionVersion>1</orderActionVersion>
</SearchByOrderAction>
</OrderDetails>
</OrderSearchCriteria>
<provisioningDetailsIndicator>true</provisioningDetailsIndicator>
</InquireEnterpriseOrderDataRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
This should generate the required response using SOAP::Lite request.
use strict;
use warnings;
use SOAP::Lite +trace=>'all';
$on_action = '';
$proxy = 'http://serviceURL';
$soap = SOAP::Lite->new(proxy => $proxy);
$soap->on_action(sub {$on_action});
$soap->readable(1);
$soap->autotype(0);
$soap->serializer->register_ns('http://mycompany.com/InquireEnterpriseOrderDataRequest.xsd' => 'xsi');
$soap->serializer->register_ns('http://mycompany.com/InquireEnterpriseOrderDataRequest.xsd' => 'xsi:schemaLocation');
$soap->default_ns('http://mycompany.com/InquireEnterpriseOrderDataRequest.xsd');
$soap->envprefix('SOAP-ENV');
$sheader = SOAP::Header->name(MessageHeader =>\SOAP::Header->value(SOAP::Header->name(TrackingMessageHeader => \SOAP::Header->value(
SOAP::Header->name(version => 111),
SOAP::Header->name(originalVersion => ''),
SOAP::Header->name(messageId => ''),
SOAP::Header->name(originatorId => 'ABC'),
SOAP::Header->name(responseTo => ''),
SOAP::Header->name(returnURL => ''),
SOAP::Header->name(timetoLive => 360000),
SOAP::Header->name(conversationId => '9AF0E9281A524262980F5284F4C57888_CCE423E277C74FA9A84D2155CD612EB3_0'),
SOAP::Header->name(routingRegionOverride => ''),
SOAP::Header->name(dateTimeStamp => '2017-04-11T18:47:53Z'),
SOAP::Header->name(timetoLive => 'mytransid'),
))->prefix('ns2')))->attr({'xmlns:ns2' => 'http://mycompany.com/MessageHeader.xsd',xmlns => 'http://mycompany.com/CingularDataModel.xsd'})->prefix('ns2');
push #request,(
SOAP::Data->name(OrderSearchCriteria => \SOAP::Data->value(
SOAP::Data->name(OrderDetails => \SOAP::Data->value(
SOAP::Data->name(SearchByOrderAction => \SOAP::Data->value(
SOAP::Data->name(orderActionNumber => 12345654),
SOAP::Data->name(orderActionVersion => 1),
)))))));
$reply = $soap->InquireEnterpriseOrderDataRequest($sheader,#request);
I'm trying to figure out why my GetOrders function for the eBay API isn't working. Below are the headers I'm passing (this is in Perl):
$objHeader->push_header('X-EBAY-API-COMPATIBILITY-LEVEL' => $compatabilityLevel);
$objHeader->push_header('X-EBAY-API-DEV-NAME' => $devID);
$objHeader->push_header('X-EBAY-API-APP-NAME' => $appID);
$objHeader->push_header('X-EBAY-API-CERT-NAME' => $certID);
$objHeader->push_header('X-EBAY-API-CALL-NAME' => 'GetOrders');
$objHeader->push_header('X-EBAY-API-SITEID' => '3');
$objHeader->push_header('Content-Type' => 'text/xml');
...and the XML I'm passing is as follows:
<?xml version="1.0" encoding="utf-8" ?>
<GetOrdersRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<DetailLevel>ReturnAll</DetailLevel>
<NumberOfDays>3</NumberOfDays>
<OrderRole>Seller</OrderRole><OrderStatus>Active</OrderStatus>
<RequesterCredentials><eBayAuthToken>$userToken</eBayAuthToken></RequesterCredentials>
</GetOrdersRequest>
(obviously $userToken is replaced with my actual user token)
I get a return from it:
$VAR1 = {
'xmlns' => 'urn:ebay:apis:eBLBaseComponents',
'Build' => 'E929_CORE_APIXO_17568878_R1',
'PageNumber' => '1',
'PaginationResult' => {
'TotalNumberOfPages' => '0',
'TotalNumberOfEntries' => '0'
},
'OrderArray' => {},
'Ack' => 'Success',
'HasMoreOrders' => 'false',
'Timestamp' => '2015-06-29T09:49:25.963Z',
'Version' => '929',
'ReturnedOrderCountActual' => '0',
'OrdersPerPage' => '100'
};
..but as you can see, no results. I know for a fact there are results (I've had it working with the PHP API already using the same kind of values as far as I can tell). Worst case scenario, I could create a basic PHP script to grab the results, and then pipe into the Perl script. Obviously this isn't ideal though (I would much prefer to have it all in one programming language)
Anyone got any ideas? I'm drawing a blank on it :/
Ok, I knew this would happen. 2 days of battling with this, and then as soon as I post something - I find the solution :)
The issue was that I was passing the following in the XML:
<OrderStatus>Active</OrderStatus>
It in fact needed to be:
<OrderStatus>Completed</OrderStatus>
That is now grabbing them perfectly :)
I need to generate a google analytic event from my perl cron job.
Based on https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide
POST to www.google-analytics.com/collect will accept the metrics.
I tried posting metric using following code, but I am not seeing them in google analytics realtime page.
use LWP::UserAgent;
$ua=LWP::UserAgent->new;
$ua->default_header("Content-type" => "application/x-rm-urlencoded");
my $response = $ua->post("http://www.google-analytics.com/collect",
"v" => 1,
"tid" => "UA-XXXXXXXX-Y",
"cid" => 125,
"t" => "pageview",
"dh" => "Cron",
"dp" => "Cron.php",
"dt" => "CronTitle");
print $response->status_line;'
Response is "200 OK", but I am not seeing the data in google analytics webpage.
I am able to post analytics from my php scripts, but could not post analytics from perl script.
I solved my problem as follows.
Created an utility php script and called that php script from my perl script. Then it worked.
<?php
$ec = $_GET['ec'];
$ea = $_GET['ea'];
$el = $_GET['el'];
$ev = $_GET['ev'];
// Send event tracking as well.
$url = 'http://www.google-analytics.com/collect';
$event_data = array(
'v' => '1',
'tid' => 'UA-XXXXXX-Y',
'cid' => $_SERVER["REMOTE_ADDR"],
't' => 'event',
'ec' => $ec,
'ea' => $ea,
'el' => $el,
'ev' => $ev);
$event_options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($event_data),
),
);
$event_context = stream_context_create($event_options);
$result = file_get_contents($url, false, $event_context);
echo "XXX";
?>
What parameters should I use in order to get search result from only a site say abcd.com using bing 2.0 API? I am trying to fetch results in JSON format. Can anybody help?
fresh code to the masses
$query = array
(
'AppId' => <API_KEY>,
'sources' => 'Web',
'query' => 'site:www.tipografix.ro '.$keywords,
'Version' => '2.0',
'Options' => 'EnableHighlighting',
'Web.Count' => $per_page,
'Web.Offset' => $page_num,
'Web.Options' => 'DisableHostCollapsing DisableQueryAlterations'
);
$request = 'http://api.bing.net/json.aspx?'.http_build_query($query);
$response = file_get_contents($request);
$jsonobj = json_decode($response);
Not sure how the JSON format of the API works but in the query parameter put in "site:abcd.com bacon" where bacon is your original query.
I'm using the XML format so if I'd send a request to:
http://api.search.live.net/xml.aspx?Appid=________&query=site%3Aabcd.com+bacon&sources=web&web.count=5&web.offset=0