Rackspace Cloud Files - [curl] 6: Couldn't resolve host 'identity.api.rackspacecloud.com' Exception thrown - rackspace-cloudfiles

The code below in localhost works fine and in server it throws an error [curl] 6: Couldn't resolve host 'identity.api.rackspacecloud.com'
$client = new Rackspace(Rackspace::UK_IDENTITY_ENDPOINT, array(
'username' => $username,
'apiKey' => $apiKey,
'ssl.certificate_authority' => false,
'curl.options' => array('debug' => true, CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4)
));
I ping in server identity.api.rackspacecloud.com and it works fine. And I followed this documentation to try to authenticate using curl:
http://docs.rackspace.com/servers/api/v2/cs-devguide/content/curl_authentication.html
This is the request I sent across:
curl -s https://lon.identity.api.rackspacecloud.com/v2.0/tokens -X 'POST' -d '{"auth":{"RAX-KSKEY:apiKeyCredentials":{"username":"xxxx", "apiKey":"yyyyyyyy"}}}' -H "Content-Type: application/json" | python -m json.tool
It return the result.
But when I run the php code, it throws an error.. Kindly guide me to find the issue.
Edit:
$data_string = '{"auth":{"RAX-KSKEY:apiKeyCredentials":{"username":"xxxxxxx", "apiKey":"yyyyyyyyyy"}}}';//json_encode($data);
$ch = curl_init('https://lon.identity.api.rackspacecloud.com/v2.0/tokens');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
echo $result = curl_exec($ch);
if(curl_errno($ch)){
echo 'Curl error: ' . curl_error($ch);
}
I test the above code in local as well as server and in local, it works fine but not in live. change the CURLOPT_SSL_VERIFYPEER TRUE and FALSE. Nothing works.
Note:
OS: CentOS

Adding Google DNS nameserver, solves the problem.
In server, the nameserver defined in /etc/resolv.conf is
nameserver 83.138.151.80
nameserver 83.138.151.81
which is recommended nameserver by Rackspace for UK.
So the curl in command prompt works, but not work in php. So I decide to add google DNS along with it. Now the /etc/resolv.conf like this:
nameserver 83.138.151.80
nameserver 83.138.151.81
nameserver 8.8.8.8
Everything works fine...

Related

504 timeout error when sending many notifications using laravel +firebase messaging

I m sending notifications to many users at the same time and this is my code:
public function sendNotifications($title,$body,$image){
$SERVER_API_KEY ="my_api_key";
$tokens=DB::select("select * from fcm_token");
$token_array=[];
for( $i =0;$i<count($tokens);$i++){
array_push($token_array,$tokens[$i]->token);
}
$data = [
"registration_ids" =>
$token_array
,
"notification" => [
"title" => $title,
"body" => $body,
"image" => $image,
"sound"=> "default" // required for sound on ios
],
];
$dataString = json_encode($data);
$headers = [
'Authorization: key=' . $SERVER_API_KEY,
'Content-Type: application/json',
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
$response = curl_exec($ch);
curl_close($ch);
}
this function is executed after a form is filled by the admin who should give the title body and image to the back-end. after that, he must wait for the task to be complete (to make sure that everyone got the notification ). it was working like a charm before but right now after a long loading time, the server responds with 504 timeout. I know that I could have queued but would that solve the problem? wouldn't the Queue be stopped also by the same error? I m hosting the app in shared hosting (lws) and my max execution time is 60 seconds and can't increase it without paying more but I want to be sure where the problem resides.
I tried to test it with just 100 users, and it is working like a charm, but I need to send it to more than that since my users are more than 4000.
Google clound messaging supports sending to 1000 tokens at once.
$tokens=array_chunk($all_tokens,1000);
foreach($tokens as $thousand_tokens){
send_notification($thousand_tokens, $request->title.' Video added', 'New Video added in '.$cat->category->name, $cat->image_url, $data);
}
Above one is sample code. You can use that to modify your code.
function send_notification($tokens, $message, $description, $image, $data)
{
try{
$token_ids = array($tokens);
$apiKey = env('FCM_KEY');
$url = 'https://fcm.googleapis.com/fcm/send';
$msg =
array(
"registration_ids"=> $tokens,
"collapseKey"=> "com.notification",
"data"=> $data,
"notification"=> array(
"android"=> array(
"channelId"=> "Notifications-Channel",
"imageUrl"=> $image,
"sound"=> "sample.mp3"
),
"sound"=> "sample.mp3",
"channelId"=> "Notifications-Channel",
"android_channel_id"=> "Notifications-Channel",
"body"=> $description,
"title"=> $message
)
);
define("GOOGLE_API_KEY", $apiKey);
$headers = array(
'Authorization: key='.$apiKey,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($msg));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}catch(Exception $e){
return 'at exception '.$e->getLine();
die('Error: '.$e->getMessage());
}
}

cURL error 60: SSL certificate problem: certificate has expired (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)

I am using https://github.com/darthsoup/laravel-whmcs package for integration of WHMCS APIs I set up my WHMCS_API_URL, WHMCS_API_IDENTIFIER, WHMCS_API_SECRET and WHMCS_API_ACCESSKEY in .env file my Laravel Application I put code for fetching my clients by following code in my controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DarthSoup\Whmcs\WhmcsManager;
class WhmcsController extends Controller
{
//
private WhmcsManager $whmcsManager;
public function __construct(WhmcsManager $whmcsManager)
{
$this->whmcsManager = $whmcsManager;
}
public function index()
{
$result = $this->whmcsManager->client()->getClients();
dd($result);
}
}
I dump die the results but I am getting the above title error, I don't know to tackle it and even I also used cURL to fetch data
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://billing.pakchamp.com/includes/api.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt(
$ch,
CURLOPT_POSTFIELDS,
http_build_query(
array(
'action' => 'DomainWhois',
// See https://developers.whmcs.com/api/authentication
'username' => 'WHMCS_API_IDENTIFIER',
'password' => 'WHMCS_API_SECRET',
'id' => '1',
'domain' => 'exampkhkjhkjhkjlehhkuhuhuih.com',
'responsetype' => 'json',
)
)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
dd($response);
now using CURL I am getting false, please help me to solve this issue.

Using the Google Search Console API to test if a site is mobile friendly is failing with 400 errors

I want to check if a website is "mobile friendly" using the google api to access their own testing service.
I am using curl via PHP to talk to the https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run end point. Ref google api docs here.
However the returned data from my code is always a 400 error page.
What I do know is that my API key is valid. If I run the command via the curl utility in a linux shell it works (ref the example given in the google documentation).
Has anyone seen this before ? It must be something to do with my php code and how I've implemented curl in it...
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run?key=xxxx",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-Type:application/json'),
CURLOPT_POSTFIELDS => json_encode(array(
'url' => 'https://www.test.com'
))
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
So with the help of someone outside of SO I got an answer to this which was basically to setup to rewrite the code slightly differently. I can't work out what exactly is the sticking point but it works if formatted as follows...
$data="http://www.yourdomain.com/";
$url="https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run?key=xxxxx";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$payload = json_encode( array( "url"=> $data ) );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$api_content = curl_exec ($ch);
curl_close ($ch);
$api_result = json_decode($api_content);
var_dump($api_result);

Invalid domain error in gmail setting api

I want to create a label in gmail account using goggle admin sdk api.
I am sending a Http Post Request to
https://apps-apis.google.com/a/feeds/emailsettings/2.0/{domain}/{username}/label/
But i get this response :-
Invalid domain.
Error 403
Following is the code for this task :-
$domain = explode('#',$user->email);
$headers = array(
"X-HTTP-Method-Override: POST",
"Authorization: {$obj->token_type} "." {$obj->access_token}",
"Content-type: application/atom+xml",
);
$options = array(CURLOPT_URL => "https://apps-apis.google.com/a/feeds/emailsettings/2.0/$domain[1]/$domain[0]/label",
CURLOPT_HEADER => 1,
CURLOPT_RETURNTRANSFER => 1,
// CURLOPT_USERAGENT => '',
CURLOPT_FOLLOWLOCATION => 1,
// CURLOPT_VERBOSE => 1,
// CURLOPT_POST => true
);
$ch = curl_init();
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
echo "<pre>";print_r($result);echo"</pre>";
If you actually trying to access labels on your gmail.com account from your domain's admin account, it will not work. The email setting API works only on Premier and Edu Google Apps domains administrators.

Indexing documents in ElasticSearch with PHP curl

I am trying to add documents to ElasticSearch. I have no problems using curl from the command line, but I'm having trouble when using curl in PHP. I'm following this example from the docs: http://www.elasticsearch.org/guide/reference/api/index_.html
The following code is giving me this error: {"error":"IndexMissingException[[twitter] missing]","status":404}
$search_host = '127.0.0.1';
$search_port = '9200';
$index = 'twitter';
$doc_type = 'tweet';
$doc_id = 1;
$json_doc = array(
"user" => "kimchy",
"post_date" => "2012-11-15T14:12:12",
"message" => "trying out Elastic Search"
);
$json_doc = json_encode($json_doc);
$baseUri = 'http://'.$search_host.':'.$search_port.'/'.$index.'/'.$doc_type.'/'.$doc_id;
$ci = curl_init();
curl_setopt($ci, CURLOPT_URL, $baseUri);
curl_setopt($ci, CURLOPT_PORT, $search_port);
curl_setopt($ci, CURLOPT_TIMEOUT, 200);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ci, CURLOPT_FORBID_REUSE, 0);
curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'XPUT');
curl_setopt($ci, CURLOPT_POSTFIELDS, $json_doc);
$response = curl_exec($ci);
You have to set the the curl option CURLOPT_CUSTOMREQUEST to PUT, not XPUT.
curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'PUT');

Resources