I'm new to Laravel framework so I'm having a hard time to do something very trivial. The main idea is to contact an API and get its response. Below is my function where I'm having error,
public function verification($id=null){
try{
$res = $client->createRequest('POST','http://35.161.181.102/api/socialverify/linkedin',['headers' => $headers , 'body' => $urlclean]);
$res= $client->send($res);
}catch(\GuzzleHttp\Exception\RequestException $e) {
\Log::info($e->getMessage());
\Log::info($e->getCode());
\Log::info($e->getResponse()->getBody()->getContents());
}
}
When I run the above function I'm getting the error shown below,
Illegal string offset 'id'
Any pointers on what I'm doing wrong and how can I solve it.
Any help is appreciated. Thank in advance.
What do you see in your /storage/logs/laravel.log?
I assume Client is Guzzle Client and by default Guzzle throws RequestException whenever there is a request issue. See Documentation. So why not try to do this and see what's the error responded from Guzzle:
try {
$response = $client->post('http://link-to-my-api', array(
'headers' => array('Content-type' => 'application/json'),
'body' => $data
));
$response->send();
}catch(\GuzzleHttp\Exception\RequestException $e) {
\Log::info($e->getMessage());
\Log::info($e->getCode());
\Log::info($e->getResponse()->getBody()->getContents());
}
And check your /storage/logs/laravel.log to see the logs being printed.
you can try this way:
use Illuminate\Support\Facades\Http;
$response = Http::withHeaders($header)
->post($url, [
$data
]);
Related
I'm sending form data, "packed" in URL using Guzzle to my JasperReports which is installed on another server. URL consists of form data and it's super long.
All the time, I'm receiving 500 Internal Server Error.
$headers = [
'url' => $url,
];
this->client = new Client(['base_uri' => 'http://localhost:8080/jasper-r/report.jsp?id=0']);
try {
$promise = $this->client->request('POST', ["headers" => $headers]);
return response($promise->message_id);
} catch (RequestException $e) {
return abort($e->getMessage());
I try to simulate the authorization LinkedIn web browser (PHP). I use Guzzle Http Client.
Here is part of the authorization code:
use GuzzleHttp\Client as LinkedinClient;
use PHPHtmlParser\Dom as Parser;
public function authLinkedin()
{
$client = new LinkedinClient(['base_url' => 'https://www.linkedin.com']);
try {
$postData = [
'session_key' => 'My_email',
'session_password' => 'My_password',
'action' => 'login'
];
$request = $client->createRequest('POST', '/uas/login', ['body' => $postData, 'cookies' => true]);
$response = $client->send($request);
if ($response->getStatusCode() === 200) {
$parser = new Parser();
$parser->load($client->get('https://www.linkedin.com/', ['cookies' => true])->getBody());
return $parser;
} else {
Log::store("Authorization error", Log::TYPE_ERROR, $request->getStatusCode());
return null;
}
return $request;
} catch (Exception $ex) {
Log::store("Failure get followers", Log::TYPE_ERROR, $ex->getMessage());
return null;
}
}
The request is successful, returns a 200 code, but I did not authorize.
Who can faced with a similar task, or in the code have missed something. I would appreciate any advice.
I think that the issue is with CSRF protection and other hidden parameters. LinkedIn, as other sites, usually returns 200 OK for all situations, even for an error, and describes details in resulting HTML.
In your case it's better to use a web scraper, like Goutte. It emulates a user with a browser, so you don't need to worry about many things (like CSRF protection and other hidden fields). Examples can be found on the main pages, try something like this:
$crawler = $client->request('GET', 'https://www.linkedin.com');
$form = $crawler->selectButton('Sign In')->form();
$crawler = $client->submit($form, array(
'login' => 'My_email',
'password' => 'My_password'
));
You can use it with Guzzle as a driver, but some sites might require JavaScript (I'm not sure about Amazon). Then you have to go to a real browser or PhantomJS (a kind of headless Chrome).
I am trying to implement a soaprequest and making the call does seem to work. The only problem is: I don't know how to receive the response data. My code looks like this:
$auth = array(
'UsernameToken' => array(
'Username' => 'xxx',
'Password' => 'yyyy'
)
);
$header = new SoapHeader('xs','Security',$auth, 0);
$client->__setSoapHeaders($header);
$client->__setLocation('http://example.com/test.php');
$params = array(
...
'trace' => 1,
'cache_wsdl' => 0
);
try {
$response = $client->getSomeData($params);
}catch(Exception $e){
echo "Exception: ".$e->getMessage();
}
print_r($response);
This results in an empty page, because $response is empty. But the test.php file is called (I tried with a simple mail() command and it sends the mail every time I call the page with the soapclient).
So I guess the soap response is somehow sent to the test.php file - right? How do I get it? If I do not set the location, I get a nullpointerexception, so I have to do that. I tried
$client->__getLastResponse()
that's empty too.
What can I do, how do I get the soap response data? Any hints would be appreciated. Thank you!
I wrote some PHP Unit Tests, that need User Authentication for a Request.
For that i added some parameterse to createClient:
$this->client = static::createClient(array(), array(
'PHP_AUTH_USER' => TEST_USER_NAME,
'PHP_AUTH_PW' => TEST_USER_PASS,
));
TEST_USER_NAME and TEST_USER_PASS containing the Login Credentials.
If I do a request like that
$parameters = array(
"object" => self::TEST_OBJECT_ID,
);
$headers = array(
'HTTP_API_AUTHORIZATION' => 'API_AUTH_KEY',
);
$this->client->request('POST', '/api/v4/object/get', $parameters, array(), $headers);
$response = $this->client->getResponse();
$this->assertEquals(200, $response->getStatusCode(), $response->getContent());
The test says OK, but after that this Message appears:
THE ERROR HANDLER HAS CHANGED!
If i change the Credentials to something wrong, the message does not appear.
Any suggestions how i can prevent that or remove this message?
Found my mistake - i browsed all tests and regarding code and it was was the error said, i changed the error handler.
set_error_handler(array(&$this, 'handleGeoError'));
I'm testing a Silex REST service as explained here but also trying to automatically decode JSON data as is also explained in the manual but somehow it fails to create the $data parameter.
In my test I'm calling the service with:
$data = file_get_contents(__DIR__.'/resources/billing-info.json');
$client->request('POST', '/users/test_user/bills',array(), array(), array('Content-Type' => 'application/json'), $data);
and in the Controller I try to access the unmarshalled data as
$app->post('/users/{username}/bills', function(Request $request, $username) use($app) {
try {
$myData = $request->data;
.....
} catch (Exception $e){
return $app->json(array('error'=>$e->getMessage()),$e->getCode());
}
});
But the $data is non existent. What am I doing wrong?
You need to change Content-Type to CONTENT_TYPE. If you look at the source code for the Client class, you'll find that the $server argument needs to match the keys given by the $_SERVER superglobal. The content-type header is stored in the CONTENT_TYPE key.
$client->request('POST', '/users/test_user/bills',array(), array(), array('CONTENT_TYPE' => 'application/json'), $data);
Check out the Documentation on the Request-Object. I guess instead of $myData = $request->data; it must be:
$myData = $request->getContent();