Converting Curl request to Guzzle issues with cert - php

I am Trying to convert an existing phpCurl request to latest guzzle and not getting anywhere fast.
This is the current request.
$curl_opts = array(
CURLOPT_HEADER => false,
CURLOPT_HTTPHEADER => array('Content-Type: text/json', 'Content-length: '.strlen($json)),
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $json,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'https://the-domainservice.php',
CURLOPT_VERBOSE => false,
CURLOPT_SSLCERT => '/path/to/file.pem',
CURLOPT_SSLCERTTYPE => 'pem',
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1
);
$curl = curl_init();
curl_setopt_array($curl, $curl_opts);
$response = curl_exec($curl);
For Guzzle I have tried so many ways but here are a couple of example.
$response = $this->client->post('https://the-domainservice.php', [
'body' => $postData,
'cert' => '/path/to/file.pem',
'config' => [
'curl' => [
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true
]
]
]
);
And more verbose of
$request = $this->client->createRequest('POST', 'https://the-domainservice.php', [
'cert' => '/path/to/file.pem',
'verify' => false,
'headers' => [
'Content-Type' => 'text/json',
'Content-length' => strlen(json_encode($postData))
]
]);
$postBody = $request->getBody();
foreach ($postData as $key => $value) {
$postBody->setField($key, $value);
}
$response = $this->client->send($request);
For my guzzle requests I am just getting
GuzzleHttp\Exception\ServerException: Server error response [url] https://the-domainservice.php [status code] 500 [reason phrase] Internal Service Error
Really hope someone can advise.

Feel a little stupid now.
Just needed to send json data and all worked, end result was.
$response = $this->client->post('https://the-domainservice.php', [
'body' => json_encode($postData),
'cert' => '/path/to/file.pem',
]
);
Excellent stuff Guzzle !

Related

Podbean API Publish New Episode invalid type

I'm trying to publish new podcasts via the api, I've successfully upload the mp3 and jpg, which gives 600 seconds to then publish. The podcast 'type' is required and one of the acceptable values is 'public. Here the response:
{"error":"input_params_invalid","error_description":"Episode type is invalid."}
I'm doing this in PHP. I've been successfully connecting to the API, it is not a token issue.
$params = [
'access_token'=> $token,
'type' => 'public',
'apple_episode_typeoptional' => 'full',
'status' => 'publish',
'media_key' => $files['audio'],
'logo_key' => $files['image'],
'title' => 'Good day',
'content' => 'Time you <b>enjoy</b> wasting, was not wasted.'
];
$params = http_build_query($params);
// . '?' . $params,
// CURLOPT_POSTFIELDS => $params
$options = [
CURLOPT_URL => $url . '?' . $params,
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE
];
$response = $this->getPodbean($options);
UPDATE:
This is what is holding me up. I don't think I have converted curl -v -H "Content-Type: image/jpeg" -T /your/path/file.ext "{presigned_url}" correctly. I added CURLOPT_USERPWD because without it I was getting a 403, now I get 400. I added token for good measure but it doesn't seem to affect anything one way or the other.
$imageMime = mime_content_type($image_path);
$filename = basename($image_path);
$cfile = curl_file_create($image_path, $imageMime, $filename);
//Open the file using fopen.
$fileHandle = fopen($image_path, 'r');
$options = [
CURLOPT_URL => $presigned_url,
CURLOPT_USERPWD => "$this->client_id:$this->client_secret",
CURLOPT_POSTFIELDS => http_build_query(['image_upload' => $cfile, 'access_token'=> $token]),
CURLOPT_POST => TRUE,
CURLOPT_HTTPHEADER => http_build_query(['Content-Type' => $imageMime]),
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE
];
$this->getPodbean($options);

I have a curl snippet that works, however I'm getting a 400 error with Guzzle, why?

Can anyone see what I might be doing wrong here? Curl works but Guzzle (v6.3) gets a 400 error.
The Curl was generated by Postman:
Curl:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://unnamed-website.com/api2/v2/charges?chargeId=273628584&paymentGate=inovio",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"app-token: toktoktoktoktotkoktoktokt",
"Content-type: application/json",
"Authorization: Basic MDFkNWJiMTllYTdlZjVjODYzYTVjYjI3ZjUzNWY4NmM6YzFhMzRlNWJkZGYyMzljZTFmZDcwZjNiZDk0Y2Q4ZjA="
),
));
$response = curl_exec($curl);
Output:
Guzzle:
$guzzle = new Client();
$request = $guzzle->get("https://unnamed-website.com/api2/v2/charges?chargeId=273628584&paymentGate=inovio",
[
'headers' => [
"app-token" => "toktoktoktoktotkoktoktokt",
"Content-type" => "application/json",
"Authorization" => "Basic MDFkNWJiMTllYTdlZjVjODYzYTVjYjI3ZjUzNWY4NmM6YzFhMzRlNWJkZGYyMzljZTFmZDcwZjNiZDk0Y2Q4ZjA="
]
]);
$response = $request->send();
Output:
Client error response
[status code] 400
[reason phrase] Bad Request
I've even tried adding thing like this:
$request = $guzzle->get("https://unnamed-website.com/api2/v2/charges?chargeId=273628584&paymentGate=inovio",
[
'curl' => [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
],
'headers' => [
"app-token" => "toktoktoktoktotkoktoktokt",
"Content-type" => "application/json",
"Authorization" => "Basic MDFkNWJiMTllYTdlZjVjODYzYTVjYjI3ZjUzNWY4NmM6YzFhMzRlNWJkZGYyMzljZTFmZDcwZjNiZDk0Y2Q4ZjA="
]
]);
Guzzle can be call as follows:
<?php
$client = new GuzzleHttp\Client();
$response = $client->request('GET',"https://unnamed-website.com/api2/v2/charges?chargeId=273628584&paymentGate=inovio",[
'headers' => [
"app-token" => "toktoktoktoktotkoktoktokt",
"Content-type" => "application/json",
"Authorization" => "Basic MDFkNWJiMTllYTdlZjVjODYzYTVjYjI3ZjUzNWY4NmM6YzFhMzRlNWJkZGYyMzljZTFmZDcwZjNiZDk0Y2Q4ZjA="
]
]);
$response = $request->send();
$result = json_decode($response->getBody()->getContents());
?>
My fellow developers dug into this when I posted it on slack and they found the problem. I was using use Guzzle\Http\Client; when I should have been using use GuzzleHttp\Client; ... the former being was available due to a lot of unmanaged vendor packages laying around inside a giant proprietary framework.

Mailgun api response to validate mail

I have been using the mail API to validate emails, but it has not returned the response. I am doing something wrong?
curl_setopt_array($this->curl, array(
CURLOPT_URL => "https://api.mailgun.net/v4/address/validate?address=".$email,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "address=".$email,
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache"
"Authorization: Basic " . base64_encode("api:mikey")
),
));
$result=curl_exec ($this->curl);
error_log("result ". print_r($result, true));
$err = curl_error($this->curl);
curl_close($this->curl);
error_log("url: ".print_r($url, true));
error_log("error: ".print_r($err, true));
error_log("result: ".print_r($result, true));
return $result;
Well, I faced this same problem but using Guzzle 6.
You should put your account domain at the end of the API Base URL:
https://api.mailgun.net/v4/mysite.com
In Guzzle, these are my configurations (scripts in PHP):
$http_client = new Client([
'base_uri' => 'https://api.mailgun.net/v4/mysite.com',
'allow_redirects' => true,
'debug' => true, // for development purposes
'http_errors' => false
]);
This is the GET Request call:
$response = $http_client->request(
'get',
'address/validate',
[
'query' => ['address' => 'mail#mail.com']
'auth' => ['api', 'your_account_api_private_key'];
]
);

curl_exec return an XMLFault from Paypal REST Api

I'm currently using the Paypal REST Api for a mobile app.
Here's my code :
$paymentDatas = array(
"intent" => "sale",
"redirect_urls" => array(
"return_url" => "http://example.com/your_redirect_url/",
"cancel_url" => "http://example.com/your_cancel_url/"
),
"payer" => array("payment_method" => "paypal"),
"transactions" => array(
"transactions" => array(
"total" => ".99",
"currency" => "USD"
)
)
);
$paymentUrl = 'https://api.sandbox.paypal.com/v1/payments/payment';
$initCurl = curl_init();
curl_setopt_array(
$initCurl, array(
CURLOPT_URL => $paymentUrl,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false, //todo: For testing purpose, need tp be removed
CURLOPT_POST =>true,
CURLOPT_HTTPHEADER => array(
'Accept-Language: en_US',
'Accept: application/json',
'Authorization: Bearer '.$data['access_token']
),
CURLOPT_POSTFIELDS => json_encode($paymentDatas),
)
);
$initRet = curl_exec($initCurl);
dd($initRet);
curl_close($initCurl);
And the dd gives me this :
string '<ns1:XMLFault xmlns:ns1="http://cxf.apache.org/bindings/xformat"><ns1:faultstring xmlns:ns1="http://cxf.apache.org/bindings/xformat">java.lang.NullPointerException</ns1:faultstring></ns1:XMLFault>' (length=196)
I've already made others functions for authentication but I'm pretty new with REST and Paypal Api.
I had the same problem.
When changing the POST request to GET everything worked fine. Hope this will help you.

Facebook Debug Tool via cURL

I'm trying to use the Facebook Linter programmatically, so that when I update a post it automatically pings FB to pull the new open graph information.
All the solutions I'm finding online don't seem to work anymore though, and are all old.
Is there a way to do this?
This is what I have so far which doesn't work
$params = array(
'id' => $url,
'scrape' => 'true',
'access_token' => '12345|987654321'
);
$ch = curl_init("https://graph.facebook.com?" . http_build_query($params));
curl_setopt_array($ch, array(
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
));
$result = curl_exec($ch);
Add CURLOPT_POST to your CURL request so that the data is POSTed to the Graph API:
curl_setopt_array($ch, array(
CURLOPT_HEADER => 0,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
));
See the documentation here.

Categories