Zend_Http_Client POST 'missing required parameters' - web-services

First of all, I looked at this question and tried the solution, did not work for me.
I am working in Zend 1.12 on a PHP 7.0.22 server.
I have a function that should post data to an API and receive a confirmation of the post.
Whenever I test it in my rest client, in firefox, I get this response from the API call:
"{\"code\":400,\"text\":\"Missing required parameters.\"}".
When I add a header: Content-Type: application/x-www-form-urlencoded I get the expected respone: '200, true'. But even when trying to set this header in my code explicitly, it still always returns 400.
Controller:
$params = array();
$params[ 'name' ] = 'John';
$params[ 'email' ] = 'john#example.com';
$client = new Zend_Http_Client();
$client->setUri( 'path/to/my/api' );
$client->setParameterPost( $params );
$response = client->request( Zend_Http_Client::POST );
var_dump( $response );
I have tried to work with raw data aswell..
$params = array();
$params[ 'name' ] = 'John';
$params[ 'email' ] = 'john#example.com';
$params = json_encode( $params );
$client = new Zend_Http_Client();
$client->setUri( 'path/to/my/api' );
$client->setRawData( $params, 'application/json' );
$response = client->request( Zend_Http_Client::POST );
var_dump( json_decode( $response ) );
When I try to dump the $request with the post parameters inside my API (becuase I wrote the API myself), I see that this is null. Like there is no $post being sent or no data is actually being passed. Because the error 'missing required parameter' is untrue, all the parameters are set ( but maybe not passed / sent correctly ).
Can somebody tell me what I am missing? I have been at this all day.
Thanks!
EDIT (#shevron):
The API expects the parameters to be passed via url-encoded-form. Setting the appropriate header in the REST Client Content-Type: application/x-www-form-urlencoded makes the API return a good response. When this header is not added, I get the error.

Related

How authenticate listclients request

I try to get from PHP the information about the currently connected clients.
http://10.0.0.2:8000/admin/listclients?mount=/stream.mpeg
but this required an authenfication, which is not possible from a program. So I tried
http://10.0.0.2:8000/admin/listclients?mount=/stream.mpeg&user=xyz&pass=wert
but Icecast does not accept the user and pass. If I do it inside a browser it works?
In which way I have to provide user and pass?
Or is some additional information required?
Google did not help me.
It's not possible to put user and pass directlyin the URL. These information has to be base64 encoded and put into the header. Below you find a php example:
$url = "http://www.domain.eu:80004/admin/listclients?mount=/stream.mpeg";
$user = "source";
$pass= "password";
$opts = array('http' =>
array(
'method' => 'GET',
'header' => array ('Authorization: Basic '.base64_encode("$user:$pass"))
)
);
$context = stream_context_create($opts);
$list1 = file_get_contents($url1, false, $context);
You can use the admin password or the source password. I the example I have used the source password.

AWS - Guzzle The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method

I am using SignatureV4 aws authentication to authenticate my API call,
I am also using Guzzle/Client to call API.
By following these above, my API call works for GET method only and not working for POST method. For POST method call it show an error ( The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method )
My code :
use Aws\Credentials\CredentialProvider;
use Aws\Signature\SignatureV4;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
$request = new Request(
'POST',
'https://XXXXXXXXXXXXx.com/XXXXXx/XXXXXXx',
[
'body' => '{
"param1": "loream",
"param2": "4970",
"param3": "1",
"param4": "2.0",
"param5": "mL",
"param6": "kgs",
"param7": 0,
}'
]
);
$key['credentials'] = array(
'key' => 'XXXXXXXXXXXXXXX-access-key',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX-secret-key'
);
$credentials = call_user_func(CredentialProvider::defaultProvider(
$key
));
// Construct a request signer
$signer = new SignatureV4('execute-api',
'us-east-1'
);
// Sign the request
$request = $signer->signRequest($request, $credentials);
// Send the request
try {
$response = (new Client)->send($request);
print_r($response);
}
catch (Exception $exception) {
$responseBody = $exception->getResponse()->getBody(true);
echo $responseBody;
}
$response = (new Client)->send($request);
$results = json_decode($response->getBody());

webservice SOAP Response

i create this to get soap response this is my code
$requestParams = array(
'userName'=>'*********',
'password'=>'*******',
'customerReferenceNo' =>'100014246' ,
'CostCenterCode'=>'100014246',
'consigneeName'=>'Muhammad yousuf',
);enter code here
$client = new SoapClient('http://202.61.51.93:6265?WSDL');
$response = $client->InsertData($requestParams);
$getDetail =$response->InsertDataResult;
print_r($response);
but it show invalid service code in response what will be the issue??

Adding a tab to a fan page does not work... error: (#210) Subject must be a page

I'm trying to add a tab to a fanpage using the graph api/PHP SDK and I'm receiving an error :
(#210) Subject must be a page I've tried using both the user access_token AND the page access_token but neither work. I've tried using the page id of numerous accounts and still no go. Here is my code:
<?php
$path="/PAGE_ID/tabs/";
$access_token="ACCESS_TOKEN";
$params = array(
'app_id' => "APP_ID",
'access_token' => $access_token
);
try{
$install = $facebook->api($path, "POST", $params);
}catch (FacebookApiException $o){
print_r($o);
}
?>
And here is the error I get:
FacebookApiException Object
(
[result:protected] => Array
(
[error] => Array
(
[message] => (#210) Subject must be a page.
[type] => OAuthException
)
)
[message:protected] => (#210) Subject must be a page.
[string:Exception:private] =>
[code:protected] => 0
Thanks for any help you can provide!
If you are not limited to using the API to add your application to your page then you can follow the instructions provided by Facebook at this link :
https://developers.facebook.com/docs/reference/dialogs/add_to_page/
Essentially you can use a dialog ( see the link above ) or this direct URL to add tab apps to your page :
https://www.facebook.com/dialog/pagetab?app_id=YOUR_APP_ID&display=popup&next=YOUR_URL
Dont forget to substitute APP_ID for your app id and next for a different URL
API Call is atm bugged: https://developers.connect.facebook.com/bugs/149252845187252?browse=search_4f31da351c4870e34879109
But here is a solution for JS: OAuthException "(#210) Subject must be a page." - just do not use the library and do your own call.
I did it with PHP:
<?php
$url = 'https://graph.facebook.com/<PAGE ID>/tabs?app_id=<APP ID>&method=POST&access_token=<PAGE ACCESS TOKEN>&callback=test';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
Echo value should be something like "test(true)".

Read and Write Cookie in Symfony2

I want to store some information in the local browser cookie. After hours looking for a nice tutorial, I managed to store some data in a non-session cookie:
controller - indexAction()
$cookieGuest = array(
'name' => 'mycookie',
'value' => 'testval',
'path' => $this->generateUrl('my_route'),
'time' => time() + 3600 * 24 * 7
);
$cookie = new Cookie($cookieGuest['name'], $cookieGuest['value'], $cookieGuest['time'], $cookieGuest['path']);
$response = new Response();
$response->headers->setCookie($cookie);
$response->send();
I wonder if this is the correct way. Furthermore I tried several ways to read the cookie with the HttpFoundation Component, but without success. Is there another way than accessing the cookie via $_COOKIE['mycookie'] ?
Here is where I try to read the cookie
controller - cookieAction()
public function cookieAction($_locale, $branch, $page)
{
$response = new Response();
$cookies = $response->headers->getCookies();
var_dump($cookies);
// TODO: Get params for indexAction from cookie if available
return $this->indexAction($_locale, $branch, $page);
}
This is the correct way of setting cookie.
To read cookie already written in the browser do:
$request->cookies->get('myCookie');
But after I created cookie in the $response object:
$cookie = new Cookie('myCookie', 'contentOfMyCookie');
$response = new Response();
$response->headers->setCookie($cookie);
I call this method:
$response->headers->getCookies();
I get an array of cookies, which are to be written in the browser - not those already existing there.
Figuratively, between $request and $response there is a time of executing controller's code.
Besides, in a twig template you can use
{{ app.request.cookies.get('myCookie') }}
you thus get value of the cookie already written in the browser, not that from the $response object! Newly created cookie from the browser you can read only after having reloaded page (ajax doesn't need to reload whole page).
To sum it up, you can read cookies using $request object, and create them with $response object. (Obviously, for some reasons, you can also read $response object cookies - but these are rather rare situations).
$response->headers->getCookies();
should return an array of cookies look in ResponseHeaderBag class for more information about that function
this can be useful for someone trying to make cookies in symfony2 :
use Symfony\Component\HttpFoundation\Cookie;
Example how to use Cookies and Session:
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends Controller
{
public function indexAction()
{
// Set session value
$session = $this->getRequest()->getSession();
$session->set('test', 1);
// Get session value
$value = $session->get('test');
// Set cookie value
$response = new Response();
$cookie = new Cookie('test', 1, time()+3600);
$response->headers->setCookie($cookie);
// Get cookie value
$this->getRequest()->cookies->get('test');
}
}
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Response;
// set current active tab in cookie
$cookie = new Cookie('myProfileActiveTab', 'myaddress', strtotime('now + 60 minutes'));
$response = new Response();
$response->headers->setCookie($cookie);
$response->send();
// get current active tab from cookies
$cookies = $request->cookies;
if ($cookies->has('myProfileActiveTab')) {
$activetab = $cookies->get('myProfileActiveTab');
}
More interesting cookies information links (http_fundation component for symfony2):
Symfony2 http_fundation component
Symfony2 http_fundation api
Symfony2 http_fundation component (spanish)