Is it possible to simulate/make an XMLHttpRequest request (ajax) in symfony2 tests?
After search with "Problematic" answer, the correct syntax is:
$crawler = $client->request('GET', '/foo/', array(), array(), array(
'HTTP_X-Requested-With' => 'XMLHttpRequest',
));
The Request#isXmlHttpRequest() method simply checks if the X-Requested-With header is equivalent to XMLHttpRequest. If that's the method you're using to determine if a request is an ajax call, then you can simulate the behavior in the test client by adding the appropriate header to the request:
class FooFunctionalTest extends WebTestCase
{
$client = static::CreateClient();
$crawler = $client->request('GET', '/foo/', array(), array(), array(
'X-Requested-With' => 'XMLHttpRequest',
));
// ...
}
More information can be found about the Request object in the source code.
For POST, PUT:
$crawler = $client->request('POST', '/foo/', array('param' => 'value'), array(),
array(
'HTTP_X-Requested-With' => 'XMLHttpRequest',
));
For POST, PUT with raw JSON body:
$crawler = $client->request('POST', '/foo/', array(), array(), array(
'HTTP_X-Requested-With' => 'XMLHttpRequest',
'CONTENT_TYPE' => 'application/json',
), '{"param": "value"}');
If you are working with Symfony 3.x or 4.x this is the correct way to do it using POST method.
$data = ['some' => 'value'];
$client = static::createClient();
$client->request('POST', '/some_uri', ['data' => $data], [],; [
'HTTP_X-Requested-With' => 'XMLHttpRequest',
]);
Related
i want refactor curl method which is work with laravel Http facade and now i'm encounter some error
public function get($data)
{
$post_data = http_build_query($data, '', '&');
$sign = hash_hmac('sha512', $post_data, env('INDODAX_SECRET_KEY'));
$response = Http::withHeaders([
'Key' => env('INDODAX_API_KEY'),
'Sign' => $sign,
])->withOptions([
'form_params' => $data,
])
->withBody('post_data', $post_data)
->post(config('indodax.private.endpoint'), [
'CURLOPT_POSTFIELDS' => $post_data,
]);
return $response->collect();
}
public static function curl($data)
{
$post_data = http_build_query($data, '', '&');
$sign = hash_hmac('sha512', $post_data, env('INDODAX_SECRET_KEY'));
$headers = ['Key:'.env('INDODAX_API_KEY'),'Sign:'.$sign];
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_HTTPHEADER => $headers,
CURLOPT_URL => config('indodax.private.endpoint'),
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true,
]);
$response = curl_exec($curl);
return json_decode($response, true);
}
i think i'm missing CURLOPT_POSTFIELDS part, how to set CURLOPT_POSTFIELDS in Http facade
i'v tried with withOptions and still same
reff PHP Curl proxy option in Laravel Http Facade
I'm trying to upload multiple images in the API of Laravel but it keeps getting 500 errors.
I'm using CURLOPT PHP for the API here are the codes below.
If you have an alternative way to do this in PHP that's ok for me.
$data = array(
"images" => $request->file('input_images'),
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://sample/api/name",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
),
));
$response = curl_exec($curl);
curl_close($curl);
$result = json_decode($response);
print_r($result);
how to pass path parameter in guzzle HTTP Post request. I am having url like this - http://base_url/v1/rack/{id}/books
in my url {id} is the path parameter.
$addLibraryUrl = $base_url."v1/rack/{id}/book";
$headers["id"] = $id;
$requestContent['json'] = $data;
$client = new Client();
$response = $client->post($addLibraryUrl, [
"headers" => $headers,
"json" => json_encode($data)
]);
I dont think the latest guzzle uses URI templates, but the functionality to do the parameter replacing is still there:
$addLibraryUrl = \GuzzleHttp\uri_template($base_url. "v1/rack/{id}/book" , [
'id' => $id,
]);
Also you can just put the id into the URI yourself very easily.
$addLibraryUrl = $base_url."v1/rack/{$id}/book";
example:
blah.com/v1/rack/5/book
I solved my issue in this way:
$id = your_rack_id_value;
$client = new Client([ 'base_uri' => $base_url, ]);
$uri = 'v1/rack/.$id.'/book';
$requestEncodedData = json_encode($data);
$response = $client->post($uri, [
'body' => $requestEncodedData,
'headers' => [
'Content-Type' => 'application/json',
]
]);
If you sent form params then you need to sent them as post params like :
// Initialize Guzzle client
$client = new GuzzleHttp\Client(['headers'=> 'Some headers']);
// Create a POST request
$response = $client->request(
'POST',
'http://yoururl.com',
[
'form_params' => [
'key1' => 'value1',
'key2' => 'value2'
]
]
);
Or like in your case change 'json' to form_params:
$addLibraryUrl = $base_url."v1/rack/{id}/book";
$headers["id"] = $id;
$requestContent['json'] = $data;
$client = new Client();
$response = $client->post($addLibraryUrl, [
"headers" => $headers,
"form_params" => json_encode($data)
]);
I can't find any example of how to download a remote file using Guzzle 6.0. I need to pass headers in the GET request.
I have looked at the documentation which isn't helpful at all.
I came up with this but it still doesn't download a file
require_once('vendor/autoload.php');
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
$client = new Client();
$response = $client->request('GET', '/stream/20', [
'headers' => [
'Authorization: Token token' => 'df456g4fd564gfs65dg45s6fdg4dsf5g4sd65g',
'Cache-Control' => 'no-cache',
'Content-Type' => 'application/pdf'
],
'sink' => 'https://example.com/path/to/file',
]);
Has anyone successfully downloaded a file using request headers?
I think you got confused.
Where you have '/stream/20' is the url of where the file is you want to download.
The sink part is where you want to save your image.
Try this....
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
$client = new Client();
$resource = fopen('test.pdf', 'w');
$response = $client->request('GET', 'https://example.com/path/to/file', [
'headers' => [
'Authorization' => 'Token token=df456g4fd564gfs65dg45s6fdg4dsf5g4sd65g',
'Cache-Control' => 'no-cache',
'Content-Type' => 'application/pdf'
],
'sink' => $resource,
]);
echo 'downloaded';
I have the following code in CakePHP. I need the same code in laravel using guzzle
$url = "https://xyz?";
$query ='first_name='. $data->FirstName .'&gender=""'. '&home_phone='. $data->HomePhone.'&ip_address='. $data->IPAddress.'&last_name='. $data->LastName.'&user_defined_url='. $result;
$HttpSocket = new HttpSocket(array('ssl_verify_peer' => false, 'ssl_verify_host' => false));
$post_response = $HttpSocket->get($url,$query);
$response = explode('&',$post_response->body);
I have converted it in laravel using guzzle but doesnt work. Following my code that Ive converted in laravel:
$client = new Client(['verify' => false ]);
$post_response = $client->get($url, $query);
$response = explode('&',$post_response->body);
Note: use GuzzleHttp\Client; is written at the top of file.
Thanks in advance for the help!
Can you try like this,
$client = new Client();
$post_response = $client->request('GET', $url, [
'verify' => false,
'form_params' => [
'first_name' => $data->FirstName,
'gender' => "",
'home_phone' => $data->HomePhone,
'ip_address' => $data->IPAddress,
'last_name' => $data->LastName,
'user_defined_url' => $result
]
]);
$response = explode('&',$post_response->body);
Instead of form_params you can use query for sending the parameters as query string.