I have been using an API for a WMS which has updated to include authentication headers. I have been provided some required details but have been unable to sucessfully use the API. I have asked the developers but they are unable to help as they do not use PHP.
Previous to the last update, this would work:
$wsdl = URL_HERE;
$soapClient = new SoapClient($wsdl);
$params = array('customer' => $get_users_company->custcode_code);
$response = $soapClient->GetProducts($params);
With the authentification headers, this is what I currently have which is causing the error Authentication header missing
$wsdl = URL_HERE;
$ns = NAMESPACE_HERE;
$soapClient = new SoapClient($wsdl);
$headerbody = array('ID' => 'PROVIDED_ID_HERE', 'KEY' => 'PROVIDED_KEY_HERE');
$headers = new SOAPHeader($ns, 'AuthHeader', $headerbody);
$soapClient->__setSoapHeaders($headers);
$response = $soapClient->__soapCall("GetProducts", array('customer' => $get_users_company->custcode_code));
I'm not 100% sure I am doing this correctly, but without the last line, I get no errors and the page loads fine (No results). Am I correct in thinking the headers are being sent?
I have heard the good old, "we can't help because we are XML Gods and your little php is beneath us"...but you can still get technical support from them by speaking their XML language. Dump out your actual, raw XML and communicate with them using that - don't mention PHP.
Follow the example here and get your request. Make sure it is matching what the documentation of your API is requesting. If it is, call your technical support and show them your XML. If it isn't, then, you know what you need to fix.
When using $soapClient->__soapCall() the second parameter takes an array, and your data structure is also an array, so you maybe should be doing:
$params = array('customer' => $get_users_company->custcode_code);
$response = $soapClient->__soapCall("GetProducts", array($params));
Or just leave it as:
$response = $soapClient->GetProducts($params);
Which looks nicer.
Related
I am attempting to set some HTTP headers in a gRPC client call written in PHP. I have read all the documentation I can find for the PHP implementation of gRPC but cannot find anything specifying how to do this in PHP. From reading docs for other languages I have come to think that headers are specified in the client metadata. However, I can't find anything on how these should be formatted in php, and all the formats I try don't seem to work. Here is my current code:
$options = [
'credentials' => $this->credentialsObject,
'update_metadata' => function($metaData){
$metaData['headers'] = ['Authorization' => 'Bearer ' . $this->token];
return $metaData;
}
];
$client = new OrganizationServiceClient($this->url,$options);
$r = new \Google\Protobuf\GPBEmpty();
list($data,$status) = $client->list($r)->wait();
The response I get from that is the same as if I don't set the authorization header at all (Access Denied!), though I have been told that my user should have permission to view that resource.
I don't have access to any server logs to help with debugging on that side (though I am trying to get access to them - might be able to in the next day).
Any help or pointers would be appreciated. I'm been working on this for a few days now and feel like I've tried everything I can think of.
Thanks!
I was able to get the info I needed to solve the problem by asking a question in the grpc.io google group. Here is that thread: https://groups.google.com/forum/#!searchin/grpc-io/php%7Csort:date/grpc-io/p4-P78_EOyY/pHHR6Q5OBwAJ.
The gist of the solution is that gRPC uses HTTP2 (so different header syntax), with metadata being equivalent to headers. Below is my updated code. Here is the important line $metaData['authorization'] = ['Bearer ' . $this->token];. Notice that the $metaData array key is the same as the HTTP2 header key, and the value is an array containing the header value as a string.
$options = [
'credentials' => $this->credentialsObject,
'update_metadata' => function($metaData){
$metaData['authorization'] = ['Bearer ' . $this->token];
return $metaData;
}
];
$client = new OrganizationServiceClient($this->url,$options);
$r = new \Google\Protobuf\GPBEmpty();
list($data,$status) = $client->list($r)->wait();
I want to use drupal_http_request to upload a file to another REST API and then parse the result into JSON.
I am able to generate a "PHP client" for this in Postman but it doesn't work in Drupal. Here is what it looks like:
$client = new http\Client;
$request = new http\Client\Request;
$request->setRequestUrl('https://api.cloudmersive.com/virus/scan/file');
$request->setRequestMethod('POST');
$request->setHeaders(array(
'cache-control' => 'no-cache',
'apikey' => 'KEY_HERE'
));
$client->enqueue($request)->send();
$response = $client->getResponse();
echo $response->getBody();
There are two issues - one, PostMan doesn't actually add the code to upload the file. Second, the above code won't run in Drupal because it is making references that aren't available so it looks like I need to use the drupal_http_request function. I'm having a hard time figuring out how to actually do that since I don't use PHP much.
Any thoughts on how I could actually post a file to that endpoint using only the built-in Drupal 7 functions, e.g. drupal_http_request?
I am working on an api for a client. I have received the following information:
API Url: http://xyz-crm.example/WebAPI/Custom/project_name/XML/
Username: foobar
password: spameggs
I need to configure the PHP SOAP client for the same in non-WSDL mode. I have written the following but it does not seem to work:
$wsdl = null;
$options = array(
'uri' => 'http://xyz-crm.example/WebAPI/Custom/project_name/XML/',
'location' => 'http://xyz-crm.exmaple.com/WebAPI/Custom/project_name/XML/',
'login' => 'foobar',
'password' => 'spameggs'
);
$client = new SoapCLient($wsdl, $options);
I just want to make a successful ping to the api at first. See if things are working fine. What am I doing wrong here?
Update 1
I made the following changes:
$wsdl = null;
$options = array(
'uri' => "http://xyz-crm.example/WebAPI/Custom/project_name/XML/",
'location' => "http://xyz-crm.example/",
'Username' => "foobar",
'Password' => "spameggs",
'soap_version' => '1.2'
);
$client = new SoapClient($wsdl, $options);
$client = $client->getListings();
I get the error: looks like we got no XML document
[Edit by me, hakre: This update was done as feedback to answer #1. It changes the location option using a shortened URL (reason not given by OP) and it adds the soap_version option (as suggested in answer #1, but not as constant but as string (containing an invalid value), so there should be no wonder this creates an error, a correct option value is given in answer #1 (the SOAP_1_1 constant) and by intention, the correct value would be the SOAP_1_2 constant for this example). Error message as commented by OP was "SOAP Fault: Wrong version."]
Update 2
I tried the following but it still fails:
$listing = $client->getListings();
$request = $client->__getLastRequest();
The execution stops at the first line itself without ever going to the second one.
[Edit by me, hakre: As review has shown wrong configuration options in Update 1 already which are not addressed in Update 2 it would be a miracle if it still wouldn't fail. The execution stops because an Exception is thrown and no error/exception handling is done]
Die URI or file ending does not matter, it could even be .jpg, there is no default.
Have a look at similiar questions: Does this SOAP Fault mean what I think it means?
It would be helpful if you put the error message into the question, aswell as the XML output of your request.
try setting the SOAP Version in the array of your SoapClient instance to one of the constants (try different ones):
new SoapClient($url, array("soap_version" => SOAP_1_1,.......
or SOAP_1_2 ...
To debug the XML try the answer from Inspect XML created by PHP SoapClient call before/without sending the request
The error message of your updated question does not look like it coming from PHP, looks more like an answer from the webservice, means your request is actually working.
I am trying to use a rest api to upload a file. I;m not sure if its my code or if its the api. I am using Zend_Http_Client to upload the file.
The code I'm using I think is correct:
$tokenRequest = new Zend_Oauth_Token_
$tokenRequest = new Zend_Oauth_Token_Access();
$tokenRequest->setTokenSecret($secret);
$tokenRequest->setToken($access);
$client = $tokenRequest->getHttpClient($config);
$client->setUri($requestUrl);
$client->setMethod(Zend_Http_Client::POST);
$client->setFileUpload('C://Users//user1//Desktop//test.csv', 'bufile');
$response = $client->request('POST');
How do I get the last sent request headers?
Is this what your looking for: Zend_Http_Client->getLastRequest()
http://framework.zend.com/manual/en/zend.http.client.html#zend.http.client.accessing_last
So, I figured out how to get an access token from Google using the Zend_Oauth library in 1.10. Now lets say I want to get my contacts...
$config = array(
'consumerKey' => 'zzz',
'signatureMethod' => 'HMAC-SHA1',
'consumerSecret' => 'xxx'
);
$token = unserialize($_SESSION['GOOGLE_ACCESS_TOKEN']);
$client = $token->getHttpClient($config);
$client->setMethod(Zend_Http_Client::GET);
// $client->setParameterGet('max-results', '10000');
$gdata = new Zend_Gdata($client);
$gdata->setMajorProtocolVersion(3);
$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/default/full');
// $query->MaxResults=100;
$feed = $gdata->getFeed($query);
$feed is a lovely object with 25 contacts. But if I want to get more in a single pull, there doesn't seem to be a way of specifying max results that works.
If I uncomment client->setParameterGet it's ignored. It works if I specify $client->setUri and use $rawdata = client->request() to get the response, but then other issues crop up with handling the feed data that comes back... like getting it into GData for easy handling.
I've tried $feed = $gdata->importString($rawdata->getBody()) but while $rawdata->getBody() returns what seems to be valid XML, $feed->totalResults throws an error, while it wouldn't if I used $gdata->getFeed($query).
If I uncomment $query->MaxResults=100; use $gdata->getFeed($query) Google returns a 401 with "Unknown authorization header".
So is it possibly to specify parameters while using Zend_GData with an Oauth token? Or am I going to have to build my own requests, then use Zend_Feed (or some other XML/Feed dissector) for parsing?
I totally cannot get the whole list of contacts only 25... parameters do not seem to work using Gdata and query like this:
$http = $token->getHttpClient($oauthOptions);
$gdata = new Zend_Gdata($http, 'MY APP');
$gdata->setMajorProtocolVersion(3);
$gdata->getHttpClient()->setRequestScheme(Zend_Oauth::REQUEST_SCHEME_QUERYSTRING);
$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/default/full?max-results=10');
$query->setMaxResults(10);
$query->maxResults = 10;
$feed = $gdata->getFeed($query);
so i;m really into finding answers here as well. If either of you gets anything working. please post :-)
thanks
It's a bit tricky mixing a process meant to work with AuthSub with OAuth. I did some digging. So far I can get it to download all my contacts like this...
$client = $token->getHttpClient($config);
$client->setMethod(Zend_Http_Client::GET);
$client->setUri('http://www.google.com/m8/feeds/contacts/default/full/');
$client->setParameterGet('max-results', '10000');
$client->setParameterGet('v','3');
$bfeed = $client->request();
Looks like the primary difference between us is I specify the Feed URL in the $client->setUri('http://www.google.com/m8/feeds/contacts/default/full/'); and set my version differently. But I can get the body() property of $bfeed and it gives me 245k of XML to dissect.
My problem is that when I'm pulling down a single contact's feed via this method, I was getting an error.
I, like you, am trying to figure this out, so please reply with anything that works for you.