SoapClient with large data - php

I use SoapClient wsdl to retrive some data. It works fine if response is not too large, but if the response is too large, in my case 25 mb, it returns null.
Can anyone help me please?
Here's my code:
$opt = array('http' => array(
'header' => "MyIdClient:" . $idclient . "\r\nMySecret:" . $mysecret .
"\r\nContent-Type:text/xml; charset=utf-8"
));
$ctx = stream_context_create($opt);
$options = array(
'cache_wsdl' => WSDL_CACHE_NONE,
'stream_context' => $ctx,
'soap_version'=>SOAP_1_1,
'keep_alive' => 1,
"features" => SOAP_WAIT_ONE_WAY_CALLS
);
$client = new SoapClient($wsdl, $options);
$search_result = $client->__doRequest($request, $endpoint, $action, 1, 0);

Related

Php Soap request failed because of authentication error

The bounty expires in 6 days. Answers to this question are eligible for a +50 reputation bounty.
Porcellino80 wants to draw more attention to this question.
I'm a little bit out of my comfort zone. I'm trying to create a SOAP request with PHP, and I'm using the "in" famous API of KBO, here's the documentation:
https://economie.fgov.be/sites/default/files/Files/Entreprises/CBE/Cookbook-CBE-Public-Search-Webservice.pdf
So essentially everything went well, but I'm still struggling with something about the authentication, which makes me fail my entire small framework:
<?php
$endpoint = 'https://kbopub-acc.economie.fgov.be/kbopubws110000/services/wsKBOPub';
$wsdl = 'https://kbopub-acc.economie.fgov.be/kbopubws110000/services/wsKBOPub?wsdl';
$username = 'myusername';
$password = 'mypassword';
$timestamp = gmdate('Y-m-d\TH:i:s\Z');
$nonce = base64_encode(random_bytes(16));
$passwordDigest = base64_encode(sha1($nonce . $timestamp . $password, true));
$header = '
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>' . $username . '</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">' . $passwordDigest . '</wsse:Password>
<wsse:Nonce>' . $nonce . '</wsse:Nonce>
<wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">' . $timestamp . '</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>';
$options = array(
'soap_version' => SOAP_1_1,
'trace' => true,
'exceptions' => true,
'encoding' => 'UTF-8',
'cache_wsdl' => WSDL_CACHE_NONE,
'stream_context' => stream_context_create(array(
'http' => array(
'header' => 'Authorization: WSSE profile=' . $header,
'user_agent' => 'PHPSoapClient'
),
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false
)
))
);
$context = stream_context_create(array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
),
));
$options['stream_context'] = $context;
// $options = array(
// 'soap_version' => SOAP_1_1
// );
$client = new SoapClient($wsdl, $options);
// Set up the request parameters
$enterpriseNumber = '0810.002.854';
$request = array('EnterpriseNumber' => $enterpriseNumber);
// Call the SOAP operation with the request parameters
$response = $client->__soapCall('ReadEnterprise', array($request));
// Get the request and response XML
$requestXML = $client->__getLastRequest();
$responseXML = $client->__getLastResponse();
var_dump($response);
I also followed some suggestions from my PHP log file, where it was written to use a different version of the soap version: SOAP_1_1 instead of the SOAP_1_2.
What I find really challenging is the fact that I can't debug it in any way, I have no idea on how I can check the error message, or I can't have any hint in my error log file, it simply mentions the security check, but nothing much.
Has anyone had the same issue?
THanks
By the way, the error that I have encountered is the following:
[14-Feb-2023 17:09:24 UTC] PHP Fatal error: Uncaught SoapFault exception: [ns1:SecurityError] A security error was encountered when verifying the message in
Stack trace:
#0 /Users/mymac/Sites/cboxform/api-call/index.php(55): SoapClient->__soapCall('ReadEnterprise', Array)
#1 {main}
thrown in /Users/mymac/Sites/cboxform/api-call/index.php on line 55

Guzzle3 send raw Post request

I am working on project that uses Guzzle3 (v3.9.3), I would like to know how to send a raw post request, I have tried these solutions but none of them worked for me. I am using this Guzzle3 manual.
Solution :
$client = new Client();
$client->setDefaultOption('headers', array(
'Authorization' => 'Bearer '.$token,
'Accept' => 'application/json'
));
$body = '{"filter_":{"user":{"email":"aaa#test.com"}}}';
$req = $client->post($url, array(), $body,
array(
'cert' => array($certification, 'password'),
)
);
$response = json_decode($client->send($req)->getBody(true));
Solution 2 :
$client = new Client();
$client->setDefaultOption('headers', array(
'Authorization' => 'Bearer '.$token,
'Accept' => 'application/json'
));
$body = '{"filter_":{"user":{"email":"aaa#test.com"}}}';
$req = $client->post($url, array(), array(),
array(
'cert' => array($certification, 'password'),
)
);
$req->setBody($body);
$response = json_decode($client->send($req)->getBody(true));
None of them worked for me,
Error : Client error response [status code] 404 [reason phrase] Not Found [url]
I have tried some solutions found in the internet (but for Guzzle6) it works but I don't get the right results (it doesn't take in consideration the filter that I have sent , which is the mail address, so I get all results)
...
$body = array(
'filter_' => array(
'user' => array( "email" => $email )
)
);
$req = $client->post($url, array(),array('body'=> $body),
array(
'cert' => array($certification, 'password'),
)
);
...
On postman the call to WS work.
Thanks in advance
I'm posting the response in case someone need , I had to put all the bloc between try catch
try{
$client = new Client();
$client->setDefaultOption('headers', array(
'Authorization' => 'Bearer '.$token,
));
$body = '{"filter_":{"user":{"email":"aaa#test.com"}}}';
$req = $client->post($url, array(), $body,
array(
'cert' => array($certification, 'password'),
)
);
$response = json_decode($client->send($req)->getBody(true));
catch(Guzzle\Http\Exception\BadResponseException $e){
$response = json_decode($e->getResponse()->getBody(),true);
}

Pritunl Rest Api using Php

Has anyone managed to access pritunl's api using php?
The guide provided from the source code https://github.com/pritunl/pritunl/blob/master/tools/test_pritunl.py has only python examples.
If I try do read data, it works fine. But when I try to update or create a new entity I receive HTTP request failed! HTTP/1.1 401 UNAUTHORIZED
My code is:
public static function auth_request($method, $path, $data="")
$BASE_URL = env('PRITUNL_BASE_URL');
$API_TOKEN = env('PRITUNL_API_TOKEN');
$API_SECRET = env('PRITUNL_API_SECRET');
$auth_timestamp = strval(time());
$auth_nonce = uniqid();
$auth_array = array(
$API_TOKEN,
$auth_timestamp,
$auth_nonce,
strtoupper($method),
$path,
);
if ($data) {
array_push($auth_array, $data);
}
$auth_string = join("&", $auth_array);
$auth_signature = base64_encode(hash_hmac(
"sha256", $auth_string, $API_SECRET, true));
$options = array(
"http" => array(
"header" => array(
'Content-Type: application/json',
'Auth-Token: '.$API_TOKEN,
'Auth-Timestamp: '.$auth_timestamp,
'Auth-Nonce: '.$auth_nonce,
'Auth-Signature: '.$auth_signature
),
"method" => $method,
"content" => $data,
),
"ssl" => array(
"allow_self_signed" => true,
"verify_peer_name" => false,
),
);
$context = stream_context_create($options);
return file_get_contents($BASE_URL.$path, false, $context);
Solved removing the $data from $auth_array.
This is my new code:
public static function auth_request($method, $path, $data="") {
$BASE_URL = env('PRITUNL_BASE_URL');
$API_TOKEN = env('PRITUNL_API_TOKEN');
$API_SECRET = env('PRITUNL_API_SECRET');
$auth_timestamp = strval(time());
$auth_nonce = uniqid();
$auth_array = array(
$API_TOKEN,
$auth_timestamp,
$auth_nonce,
strtoupper($method),
$path,
);
$auth_string = join("&", $auth_array);
$auth_signature = base64_encode(hash_hmac(
"sha256", $auth_string, $API_SECRET, true));
$options = array(
"http" => array(
"header" => array(
'Content-Type: application/json',
'Auth-Token: '.$API_TOKEN,
'Auth-Timestamp: '.$auth_timestamp,
'Auth-Nonce: '.$auth_nonce,
'Auth-Signature: '.$auth_signature
),
"method" => $method,
"content" => $data,
),
"ssl" => array(
"allow_self_signed" => true,
"verify_peer_name" => false,
),
);
$context = stream_context_create($options);
return file_get_contents($BASE_URL.$path, false, $context);
}

Parameters breaks OAuth

I have an array $fields with a screen_name and a count.
No matter what I try, if I add them I get an error
Could not authenticate you.
One attempt was:
$parameters = array_merge($oauth, $fields);
I'm not gonna list all attempts cause that will just be noise.
Without using the parameter fields everything works ok.
Can someone help, i'm totally lost here.
$settings = array(
'oauth_access_token' => "304291054-X3AyJrHYUswZQbXXXXXXXXXXXXXOKw53xOi9E",
'oauth_access_token_secret' => "iWuakK6iWsfXXXXXXXXXSGcW6WeqxLsiQf",
'consumer_key' => "odXXXXXXXXXXkBg6GmsTg",
'consumer_secret' => "5LoqUsXXXXXXXXXXXXXXXXi6zgmQ"
);
$base_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$http_method = 'GET';
$fields = array(
'screen_name' => 'wearethegun',
'count' => '5'
);
$oauth = array(
'oauth_consumer_key' => $settings['consumer_key'],
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $settings['oauth_access_token'],
'oauth_timestamp' => time(),
'oauth_version' => '1.0'
);
// todo, fields
$parameters = $oauth;
ksort($parameters);
$base = $http_method.'&'.
rawurlencode($base_url).'&'.
rawurlencode(http_build_query($parameters));
$key = rawurlencode($settings['consumer_secret']).'&'.
rawurlencode($settings['oauth_access_token_secret']);
$signature = base64_encode(hash_hmac('sha1', $base, $key, true));
$signature = rawurlencode($signature);
$oauth_header = '';
foreach ($oauth as $key => $value) {
$oauth_header .= $key.'="'.$value.'", ';
}
$oauth_header .= 'oauth_signature="' . $signature . '"';
$curl_options = array(
CURLOPT_HTTPHEADER => array("Authorization: Oauth {$oauth_header}", 'Expect:'),
CURLOPT_HEADER => false,
CURLOPT_URL => $base_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
);
$curl_request = curl_init();
curl_setopt_array($curl_request, $curl_options);
$json_str = curl_exec($curl_request);
curl_close($curl_request);
$json = json_decode($json_str, true);
echo "<pre>";
print_r($json);
echo "</pre>";

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);

Categories