I'm trying to upload an image using guzzlehttp/guzzle v7.2. The curl command works correctly as:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://31.207.77.245/services/update?id=' . $s2jsonResponse->hits[0]->id,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('Filedata'=> new CURLFILE($hit->metadata->path)),
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer ' . $authToken
),
));
$response = curl_exec($curl);
curl_close($curl);
while when using guzzle:
$client = new GuzzleHttp\Client();
$headers = [];
$headers['Authorization'] = 'Bearer ' . $authToken;
$request = $client->request('POST', 'http://31.207.77.245/services/update?id=' . $s2jsonResponse->hits[0]->id,
['multipart' =>[
[
'name' => 'Filedata',
'contents' => fopen($hit->metadata->filename, 'rb'),
'headers' => $headers
]
]]);
$urequest->getBody();
it fails with the error message:
Fatal error: Uncaught GuzzleHttp\Exception\RequestException: cURL error 55: Send failure: Broken pipe (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://31.207.77.245/services/update?id=B9l8TjX1a0A93YMSF6IDoH in /Users/augusto/Documents/assets/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:211
It worked as:
$client = new GuzzleHttp\Client();
$headers = [];
$headers['Authorization'] = 'Bearer ' . $authToken;
$request = $client->request('POST', 'http://31.207.77.245/services/update?id=' . $s2jsonResponse->hits[0]->id, [
'headers' => $headers,
'multipart' => [
[
'name' => 'Filedata',
'contents' => fopen($hit->metadata->filename, 'rb')
]
]]);
$urequest->getBody();
Related
I have the following code from PayPal Payout SDK to get access tokens from the PayPal API.
curl -v POST https://api-m.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "CLIENT_ID:SECRET" \
-d "grant_type=client_credentials"
To get the access token, I have tried the following.
$client_id = "AWN5555";
$secret = "44444";
$url = "https://api-m.sandbox.paypal.com/v1/oauth2/token";
$data = ['grant_type:client_credentials'];
$response = Http::withHeaders([
'Accept:application/json',
'Accept-Language:en_US',
"Content-Type: application/x-www-form-urlencoded"
])->withBasicAuth($client_id, $secret)
->post($url, $data);
// OR
$response = $client->request('POST', $url, [
'headers' => [
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Authorization ' => ' Basic ' .
base64_encode($client_id . ':' . $secret)
],
'form_params' => [
'grant_type' => 'client_credentials',
]
]);
laravel 7 or 8 solution :
$client_id = "AWN5555";
$secret = "44444";
$url = "https://api-m.sandbox.paypal.com/v1/oauth2/token";
$data = [
'grant_type' => 'client_credentials',
];
$response = Http::asForm()
->withBasicAuth($client_id, $secret)
->post($url, $data);
php native solution :
$client_id = "AWN5555";
$secret = "44444";
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api-m.sandbox.paypal.com/v1/oauth2/token',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'grant_type=client_credentials',
CURLOPT_HTTPHEADER => [
'Authorization: Basic '.base64_encode($client_id.':'.$secret)
],
]);
$response = curl_exec($curl);
curl_close($curl);
This is the error I'm getting, as you can see there is a parameter in the URL, but the error says there weren't any parameters given. Can anbody help me out?
Client error: PUT https://webapi.teamviewer.com/api/v1/devices/d38237721?alias=laptop-test resulted in a 400 Bad Request response:
{"error":"invalid_request","error_description":"no parameters were given.","error_code":1}
This is my code
public function update($device_id, $options)
{
$token = 'thereisatokenhere';
$client = new Client(['base_uri' => 'https://webapi.teamviewer.com/api/v1/']);
$headers = [
'Authorization' => 'Bearer ' . $token,
'Accept-Language' => 'en-US',
'Content-Type' => 'application/json'
];
$response = $client->request('PUT', 'devices/' . $options['device_id'], [
'headers' => $headers,
'form_params' => [
'alias' => $options['alias'],
],
]);
$response = json_decode($response->getBody()->getContents(), true);
$deviceIdsAPI = $response['devices'];
return $deviceIdsAPI;
}
2nd
$request = new Request('PUT', 'https://webapi.teamviewer.com/api/v1/devices/' . $options['device_id'], ['alias' => $options['alias']]);
$response = $client->send($request, ['timeout' => 2, 'headers' => $headers]);
Here is an example of a PUT request in Guzzle:
$client->put('devices/' . $options['device_id'], [
'body' => [
'alias' => $options['alias'],
'other_field' => '123'
],
'headers' => $headers,
'allow_redirects' => false,
'timeout' => 5
]);
Update:
In the latest version (Guzzle 6) it should be like this:
use GuzzleHttp\Psr7\Request;
$request = new Request('PUT', 'http://httpbin.org/put', ['test' => '123']);
$response = $client->send($request, ['timeout' => 2, 'headers' => $headers]);
See this answer and here is the official Guzzle documentation
I have a curl code like this which I am trying to convert into guzzle like so
$response = $client->post(self::$url, [
'query' => array(
'app_id' => "app-id",
'included_segments' => array('All'),
'contents' => $content,
'headings' => $headings) ],
['headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Basic api key'
]
]);
But when I try to run this I get this error
...` resulted in a `400 Bad Request` response:\n{\"errors\":[\"Please include a case-sensitive header of Authorization: Basic <YOUR-REST-API-KEY-HERE> with a valid REST AP (truncated...)
CURL
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8','Authorization: Basic api key'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
Which version of Guzzle is this? Because the latest is different.
$client = new GuzzleHttp\Client();
$req = $client->request('POST', self::$url, [
'json' => ['app_id' => '...', 'foo' => 'bar'],
'headers' => ['Authorization' => 'Basic api key']
]);
$res = $client->getBody()->getContents();
I'm pretty sure that 'json' adds automatically the specific header, otherwise transform 'json' in 'form_params' and add the header (content-type).
I have a curl request like the following in Codeigniter :
$order = [
'index' => 'Value',
'index2' => 'Value2'
];
$this->curl->create($this->base_url.'order/');
$this->curl->http_login($creds['username'], $creds['password']);
$this->curl->ssl(TRUE, 2, 'certificates/certificate.pem');
$this->curl->option(CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json'));
$this->curl->option(CURLOPT_FAILONERROR, FALSE);
$this->curl->post(json_encode($order));
$data = $this->curl->execute();
Now I need to issue same request in Laravel, where I am using Guzzle. How can I convert this to a Guzzle request ?
Very, very easy:
$client = new GuzzleHttp\Client(['base_uri' => $this->base_url]);
$response = $client->request('POST', 'order/', [
'form_params' => $order,
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json'
],
'auth' => [$creds['username'], $creds['password']],
'http_errors' => false,
'verify' => 'certificates/certificate.pem'
]);
echo $response->getBody();
Note that this has nothing to do with Laravel, it's just Guzzle. Laravel doesn't affect Guzzle API in any way.
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();