SoapClient PHP application/soap+xml; - php

How to set the content type so that the following code does not return an error:
Cannot process the message because the content type 'application/soap+xml; charset=utf-8; action="http://webservice.comarchedi.com/ComarchEdiWebService/SubscriptionGet"' was not the expected type 'text/xml; charset=utf-8'.
$url = 'https://edi.edoc-online.com/EdiWebService/EdiWebService.svc?wsdl';
$stream_context_opts = [
'http' => [
'method' => 'GET',
'header' => 'application/soap+xml; charset=utf-8'
]
];
$soap_stream_context = stream_context_create($stream_context_opts);
try {
$client = new SoapClient($url, [
'soap_version' => SOAP_1_2,
'trace' => true,
'encoding' => 'UTF-8',
'stream_context' => $soap_stream_context
]);
debug($client->__getFunctions());
debug($client->SubscriptionGet($login, $password));
} catch (\Exception $ex) {
echo $ex->getMessage();
}

Related

How can I get response from guzzle in Laravel 5.3

I try like this :
$client = new Client();
$res = $client->request('POST', 'https://api.orange.com/smsmessaging/v1/outbound/tel:+phone/requests/', [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization'=>'Bearer '.$token,
/*'Content-Type' => 'application/x-www-form-urlencoded',*/
],
/*'form_params' => $body ,*/
'json' => [
'outboundSMSMessageRequest'=>[
'address'=> 'tel:+$phone',
'senderAddress'=>'tel:+phone_rec',
'outboundSMSTextMessage'=>[
'message'=> 'Hello test!'
]
]],
'debug' => true,
'verify' => false,
]
);
$res->getStatusCode();
// 200
$res->getHeader('content-type');
// 'application/json; charset=utf8'
$res->getBody();
When executed, the result is an errror curl_setopt_array(): cannot represent a stream of type Output as a STDIO FILE*
How can I get the response?
I try in postman, it success get response
But I try use guzzle, it failed
You can try code below:
try {
$client = new Client();
$token = 'token';
$res = $client->request('POST', 'https://api.orange.com/smsmessaging/v1/outbound/tel:+phone/requests/', [
'headers' => [
'Content-Type' => 'application/json',
'Authorization'=>'Bearer '. $token,
],
'json' => [
'outboundSMSMessageRequest'=>[
'address'=> "tel:youre-phone",
'senderAddress'=>'tel:+phone_rec',
'outboundSMSTextMessage'=>[
'message'=> 'Hello test!'
]
]],
'debug' => true,
'verify' => false,
]
);
echo $res->getBody();
} catch ( \GuzzleHttp\Exception\ClientException $exception ) {
echo $exception->getResponse()->getBody();
}
I resolved it like this
$requestContent = [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization'=>'Bearer '.$token,
],
'json' => [
'outboundSMSMessageRequest'=>[
'address'=> "tel:youre-phone",
'senderAddress'=>'tel:+phone_rec',
'outboundSMSTextMessage'=>[
'message'=> 'Hello test !'
]
]
]
];
try {
$client = new Client();
$res = $client->request('POST', 'https://api.orange.com/smsmessaging/v1/outbound/tel:+phone_rec/requests/', $requestContent);
$response = json_decode($res->getBody());
dd($response);
} catch (RequestException $re) {
}

How to prevent http redirects when using SoapClient?

I am using standard SoapClient class to make requests to the payment server.
$context = stream_context_create([
'http' => [
'follow_location' => 0,
],
]);
$options = [
'uri' => 'urn:PaymentServer',
'location' => 'http://localhost:8001/index.php',
'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
'trace' => true,
'soap_version' => SOAP_1_1,
'stream_context' => $context,
'connection_timeout' => 50,
];
$soapClient = new SoapClient(null, $options);
try {
var_export($soapClient->GetPayment([]));
} catch (Throwable $e) {
echo $e->getMessage();
var_export($soapClient->__getLastResponse());
}
Script http://localhost:8001/index.php contains following code
header('Location: http://example.com', true, 302);
exit;
I expect empty result, but the actual output contains HTML from example.com
I have tried get_file_contents function with the same stream context:
var_export(file_get_contents('http://localhost:8001/index.php', false, $context));
And I have got empty string.
What SoapClient or stream context option should be set to prevent redirects?

Sending HTTP Post Request to site with array in Body

I'm trying to make a POST request and send some values in the body of an API call. In the documentation of the API it says I need to make a POST request, using startUrls as an array with key and value.
<?php
$url = 'https://api.apify.com/v1/USERID/crawlers/CRAWLERID/execute?token=TOKENID';
$postData = array(
'startUrls' => array(array('key'=>'START', 'value'=>'https://instagram.com/instagram'))
);
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/json',
'body' => json_encode($postData)
)
));
$resp = file_get_contents($url, FALSE, $context);
print_r($resp);
?>
The JSON seems to be how it should, but the script is not sending the body properly to the website.
According to the documentation, there is no body option for the HTTP context. Try content instead:
<?php
$url = "https://api.apify.com/v1/USERID/crawlers/CRAWLERID/execute?token=TOKENID";
$postData = [
"startUrls" => [
["key"=>"START", "value" => "https://instagram.com/instagram"]
]
];
$context = stream_context_create([
"http" => [
"method" => "POST",
"header" => "Content-type: application/json",
"content" => json_encode($postData)
]
]);
$resp = file_get_contents($url, FALSE, $context);
print_r($resp);
The following code will work. I have set the headers and specified the content type.
$request = new HttpRequest();
$request->setUrl('$url = 'https://api.apify.com/v1/USERID/crawlers/CRAWLERID/execute?token=TOKENID');
$request->setMethod(HTTP_METH_POST);
$request->setHeaders(array(
'cache-control' => 'no-cache',
'content-type' => 'application/x-www-form-urlencoded'
));
$request->setContentType('application/x-www-form-urlencoded');
$request->setPostFields(array('key'=>'START', 'value'=>'https://instagram.com/instagram')
));
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}
If you want to try with cUrl the following code snippet will work.
$curl = curl_init();
$postData = array(
'startUrls' => array(array('key'=>'START', 'value'=>'https://instagram.com/instagram'))
);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.apify.com/v1/USERID/crawlers/CRAWLERID/execute?token=TOKENID",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $postData,
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache",
"content-type: multipart/form-data;"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

PHP SOAP call content type

I'm attempting to connect with a WS via Soap and struggling with a content type error when I call the method: content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'
Below is my code, reducing number of params and hiding url, user and password.
The error happens when I call the ClientRegist() function. I'm convinced I have to pass $params in a different way, but can't find an example anywhere.
$url = 'http://---';
$ctx_opts = array(
'http' => array(
'header' => 'Content-Type: application/soap+xml'
),
'username' => '---',
'password' => '---',
'trace' => true,
'exceptions' => true
);
$ctx = stream_context_create($ctx_opts);
$client = new SoapClient($url, array('stream_context' => $ctx));
$params = array(
'Cardnumber' => $number,
'Name' => $name
);
try {
$client = new SoapClient($url, array('trace' => $trace, 'exceptions' => $exceptions));
$response = $client->ClientRegist($params);
}
catch (Exception $e) {
echo "Error!<br>";
echo $e -> getMessage ().'<br>';
echo 'Last response: '. $client->__getLastResponse();
}
var_dump($response);
Try SOAP version 1.2. Its default Content-Type is application/soap+xml
<?php
// ...
$client = new SoapClient(
$url,
array(
'soap_version' => SOAP_1_2, // !!!!!!!
'stream_context' => $ctx
)
);

Bigcommerce API - Creating Webhooks - Invalid Header

I'm making small steps into this project I am working on. Now creating and registering a webhook. I'm getting the below response:
400 - Invalid Header
I have tried the following code:
// Send a request to register a web hook
$http2 = new Client('https://api.bigcommerce.com', array(
'request.options' => array(
'exceptions' => false,
'headers' => array(
'X-Auth-Client' => $client_id,
'X-Auth-Token' => $access_token,
'Content-Type' => 'application/json',
'X-Custom-Auth-Header' => $access_token,
)
)
));
$request = $http2->post('/'.$store_hash.'/v2/hooks', null, array(
'scope' => 'store/order/*',
'destination' => 'https://example.com/process_order.php',
'is_active' => true
));
$response = $request->send();
$body = $response->getBody(true);
var_dump($body);
echo '<p>Status Code: ' . $response->getStatusCode() . '</p>';
... and
// Send a request to register a web hook
$http2 = new Client('https://api.bigcommerce.com', array(
'request.options' => array(
'exceptions' => false,
'headers' => array(
'X-Auth-Client' => $client_id,
'X-Auth-Token' => $access_token,
'Content-Type' => 'application/json',
)
)
));
$request = $http2->post('/'.$store_hash.'/v2/hooks', null, array(
'scope' => 'store/order/*',
'headers' => array(
'X-Custom-Auth-Header' => $access_token,
),
'destination' => 'https://example.com/process_order.php',
'is_active' => true
));
$response = $request->send();
$body = $response->getBody(true);
var_dump($body);
echo '<p>Status Code: ' . $response->getStatusCode() . '</p>';
I am working with the documentation here:
https://developer.bigcommerce.com/api/stores/v2/webhooks#create-a-hook
However, I can't seem to work out what {secret_auth_password} is as well? The documentation doesn't explain this. I am sending the Client ID and Client Header as part of the headers as well.
Still getting Invalid Header as a response.
I am using Guzzle.
Can anyone assist me on this please?
I have finally worked out what I did wrong after numerous attempts.
Answer: send the data in JSON format.
Resolved Code:
// Send a request to register a web hook
$http3 = new Client('https://api.bigcommerce.com', array(
'request.options' => array(
'exceptions' => false,
'headers' => array(
'X-Auth-Client' => $client_id,
'X-Auth-Token' => $access_token,
'Content-Type' => 'application/json',
'Accept' => 'application/json',
)
)
));
$request = $http3->post('/'.$store_hash.'/v2/hooks', null, json_encode(array(
'scope' => 'store/order/statusUpdated',
'destination' => 'https://example.com/process_order.php',
)));
$response = $request->send();

Categories