References:
http://docs.guzzlephp.org/en/stable/request-options.html#proxy
Set proxy in Guzzle
Env :
GuzzleHttp/6.2.1
curl/7.47.0
PHP/7.1.3-3+deb.sury.org~xenial+1
I am trying to use proxy server with async Guzzle calls.
I have discovered that when I set proxy when creating a client, it works.
e.g.
new Client(['proxy' => 'tcp://64.140.159.209:80'])
However when create a client with no options .. and then set proxy on Request, proxy is not used at all, and guzzle makes a direct connection from client machine to server machine. That is confirmed by hitting http://httpbin.org/ip and inspecting the Origin returned by httpbin.
I need the ability to set proxy on each request.
Here is relevant code:
$client = new Client();
$request = new Request(
'GET',
'http://httpbin.org/ip',
['proxy' => 'tcp://64.140.159.209:80']
);
$client->sendAsync($request)
->then(
...closure here
// process here
);
Hope this helps for someone.
The document http://docs.guzzlephp.org/en/stable/request-options.html#proxy only lists creating a new request from a client.
That means I understood the usage wrong. I was creating new Request directly and passing third param with proxy info expecting that to be changed per request within a single client. It looks like that proxy is set on per client basis, even if you are making asynchronous calls.
So I had to modify my application to use a new client per async request.
Related
i'm building an app using Laravel for providing some API for an android application, and i'm using some external APIs from another server (with another URL). i want to make something like proxy or tunnel for external API requests from android side but in my own URL.
for example:
android wants to request for externalUrl.com/api/objects but i want he request to this myDOmain.com/api/x/objects and get the exact same response that the first link returns, without any change.
and there is more than one external API, and i don't want to write separate code for each one.
need something like this :
Route::any('/x/{somewhere}', function($request){
return [$request, externalUrl.com/api/{somewhere}]->response;
})
i'm not asking for http request libraries! i want to redirect request to another domain and return its request.
Best option is to install Guzzle. https://github.com/guzzle/guzzle
It's really simple to use it.
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'externalUrl.com/api/{somewhere}');
return $response->getBody();
You can use Guzzle :
use Guzzle\Http\Client;
use Guzzle\Stream\PhpStreamRequestFactory;
[...]
$request = new Client("externalUrl.com/api/{$somewhere}");
$response = $request->send();
return $response->getBody();
I need to create a Webhook server like Telegram Webhook server.
I googled it but didn't find any resources!
I'm not talking about receiving Webhook requests. I'm talking about creating a complete Webhook server to send HTTP POST requests to specific URLs. And our clients could receive the requests in their URLs by :
$response = file_get_contents('php://input');
Any helps would be great appreciated.
P.S:
Sorry for my bad English.
you can try Captain Hook laravel package, which provides you to add webhook to your laravel application
What a webhook actually does is nothing more than sending a request. The most easy way to set this up is by using Guzzle (https://packagist.org/packages/guzzlehttp/guzzle).
What you need to set up is on your side a script which decides what url to call, when that happens, just create the post request via guzzle.
$postData = [];
$client = new GuzzleHttp\Client();
$response = $client->request('POST', $url, $postData);
I’m trying to invoke a WCF service (.NET) from PHP. It’s a little more complicated than just using a SoapClient since the service uses a WS2007FederationHttpBinding to authenticate.
Here’s the code I’m using at the moment. I haven’t even added credentials as I’m not sure how, but regardless, I’m not even at the point where I’m getting access denied errors.
$wsdl = "https://slc.centershift.com/sandbox40/StoreService.svc?wsdl";
$client = new SoapClient($wsdl,array(
//'soap_version'=>SOAP_1_2 // default 1.1, but this gives 'Uncaught SoapFault exception: [HTTP] Error Fetching http headers'
));
$params = array();
$params['SiteID'] = 123;
$params['GetPromoData'] = false;
$ret = $client->GetSiteUnitData(array('GetSiteUnitData_Request'=>$params));
print_r($ret);
Which WSDL should I be pointing to?
https://slc.centershift.com/Sandbox40/StoreService.svc?wsdl
Seems to be very short, but includes a reference to (note the wsdl0) https://slc.centershift.com/Sandbox40/StoreService.svc?wsdl=wsdl0
https://slc.centershift.com/Sandbox40/StoreService.svc?singleWsdl
Seems to have everything in it.
Do I need to specify SOAP 1.2? When I do, I get a connection timeout ([HTTP] Error Fetching http headers). When I don’t, the default of SOAP 1.1 is used and I get a [HTTP] Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'. Is this because I’m not authenticated yet, or because I’m using the wrong SOAP version?
How to authenticate in PHP? Here’s the corresponding .NET/C# code. Do I need to somehow put these as SOAP headers? Or am I thinking about it all wrong, and I need to do some kind of authentication before I even call the method (from what I read, I’m supposed to get a token back and then use it for all future method calls – I think I see an example of this in an answer here on Stack Overflow.
If I call $client->__getFunctions(), using either WSDL and either SOAP version, I’m getting a valid list of all functions, so I assume either of these is fine and my real issue is the authentication.
Other programmers I’ve talked to had spent time trying to get this to work, but gave up and instead implemented a proxy in .NET. They pass their parameters from PHP to their own unsecured .NET service, which in turn calls this secure service. It works, but seems crazily inefficient to me, and counter-productive, as the purpose of WCF is to support all types of clients (even non-HTTP ones!).
I’ve read How to: Create a WSFederationHttpBinding on MSDN, but it didn’t help.
You can use this URL for WSDL https://slc.centershift.com/Sandbox40/StoreService.svc?singleWsdl. This WSDL has all definitions.
You have to use 1.2 because this webservice works with SOAP 1.2 version. I tried it with 1.1 and 1.2 and both of them gived error. 1.1 is version error, 1.2 is timeout error. I think there is an error at this test server. I used it with svcutil to generate code but it gived error too. Normaly it should get information and generate the code example to call service.
Normally you can add authenticate parameters with SoapHeader or directly add to options in SoapClient consruct (if service authentication is basic authentication). I write below code according to your screenshot. But it gives timeout after long wait.
$wsdl = "https://slc.centershift.com/sandbox40/StoreService.svc?wsdl";
$client = new SoapClient($wsdl,array('trace' => 1,'soap_version' => SOAP_1_2));
$security = array(
'UserName' => array(
'UserName'=>'TestUser',
'Password'=>'TestPassword',
'SupportInteractive'=>false
)
);
$header = new SoapHeader('ChannelFactory','Credentials',$security, false);
$client->__setSoapHeaders($header);
$params = array();
$params['SiteID'] = 100000000;
$params['Channel'] = 999;
try {
$ret = $client->GetSiteUnitData($params);
print_r($ret);
}catch(Exception $e){
echo $e->getMessage();
}
__getFunctions works, because it prints functions defined in WSDL. There is no problem with getting WSDL information at first call. But real problem is communication. PHP gets WSDL, generates required SOAP request then sends to server, but server is not responding correctly. SOAP server always gives a response even if parameters or request body are not correct.
You should communicate with service provider, I think they can give clear answer to your questions.
Having worked with consuming .NET WS from PHP before I believe you would need to create objects from classes in PHP that matches the names that .NET is expecting. The WSDL should tell you the types it is expecting. I hope this assist with your path forward!
If the SOAP call works from a C# application, you could use Wireshark (with the filter ip.dst == 204.246.130.80) to view the actual request being made and then construct a similar request from php.
Check this answer to see how you can do a custom SOAP call.
There's also the option of doing raw curl requests, since it might be easier to build your xml body, but then you would have to parse the response yourself with simplexml.
I'm using Guzzle that I installed via composer and failing to do something relatively straightforward.
I might be misunderstanding the documentation but essentially what I'm wanting to do is run a POST request to a server and continue executing code without waiting for a response. Here's what I have :
$client = new \GuzzleHttp\Client(/*baseUrl, and auth credentials here*/);
$client->post('runtime/process-instances', [
'future'=>true,
'json'=> $data // is an array
]);
die("I'm done with the call");
Now lets say the runtime/process-instances runs for about 5mn, I will not get the die message before those 5mn are up... When instead I want it right after the message is sent to the server.
Now I don't have access to the server so I can't have the server respond before running the execution. I just need to ignore the response.
Any help is appreciated.
Things I've tried:
$client->post(/*blabla*/)->then(function ($response) {});
It is not possible in Guzzle to send a request and immediately exit. Asynchronous requests require that you wait for them to complete. If you do not, the request will not get sent.
Also note that you are using post instead of postAsync, the former is a synchronous (blocking) request. To asynchronously send a post request, use the latter. In your code example, by changing post to postAsync the process will exit before the request is complete, but the target will not receive that request.
Have you tried setting a low timeout?
I'm creating an API for a site that allows the user to login via the API. I'm using Guzzle, but the question is how do I use the Cookies Plugin with Guzzle? In cURL I can use a cookie file, and pass this along with requests. But the example on the Guzzle docs looks confusing.
use Guzzle\Http\Client;
use Guzzle\Plugin\Cookie\CookiePlugin;
use Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar;
$cookiePlugin = new CookiePlugin(new ArrayCookieJar());
// Add the cookie plugin to a client
$client = new Client('http://www.test.com/');
$client->addSubscriber($cookiePlugin);
// Send the request with no cookies and parse the returned cookies
$client->get('http://www.yahoo.com/')->send();
// Send the request again, noticing that cookies are being sent
$request = $client->get('http://www.yahoo.com/');
$request->send();
echo $request;
It seems to be making 3 requests. I don't understand why it's making a request to test.com, then twice to yahoo.com. Why are you not able to make 1 request?
It's just an example... You don't have to make three requests. Just attach the cookie plugin to your client and you're good.