Symfony DomCrawler status code not working? - php

hii this is my php code fore get status on url.
$client = new Client();
$url_to_traverse = 'http://wordcomat.com';
$crawler = $client->request('GET', $url_to_traverse);
$status_code = $client->getResponse()->getStatus();
but i try to found status code of facebook it's not return any thing.
$client = new Client();
$url_to_traverse = 'http://www.facebook.com';
$crawler = $client->request('GET', $url_to_traverse);
$status_code = $client->getResponse()->getStatus();
twitter not working.
$client = new Client();
$url_to_traverse = 'http://www.twitter.com';
$crawler = $client->request('GET', $url_to_traverse);
$status_code = $client->getResponse()->getStatus();
facebook or twitter or linkedin or google is not return any thing but not return any error or warning how can i got status of that links.
thank you.

thy are using https protocol
try the following
$client = new Client();
$url_to_traverse = 'https://www.facebook.com';
$crawler = $client->request('GET', $url_to_traverse);
$status_code = $client->getResponse()->getStatus();

Related

How to send a correct authorization header for basic authentication using guzzle

How to send a correct authorization header for basic authentication using guzzle
use GuzzleHttp\Client;
$username='EFDEMO';
$password='EFDEMO';
$client = new Client(['auth' => [$username, $password]]);
$res = $client->request('GET', 'https://mb-
rewards.calusastorefront.p2motivate.com/client/json.php/
getMemberAccount');
$res->getStatusCode();
$response = $res->getBody();
echo $response;
the error I am getting
{"statusCode":"Error","error":
{"errorCode":"400","errorMessage":"Authentication Header ID field must
match Basic Authentication Username"}}
Referring to documentation you should pass the auth parameter in request method instead of client's constructor:
$client = new Client();
$res = $client->request(
'GET',
'past-url-here',
['auth' => [$username, $password]]
);
I solved it like below
<?php
require '../x_static_libs/guzzle/vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();
$username = 'xxxxxx';
$password = 'xxxxxxxx';
$token = 'xxxxxxxxxxxxxxxxxxxx';
$url = 'https://ervb-rewards.com/client/json.php';
$api_token =
base64_encode(json_encode(['id'=>'xxxxxxx','token'=>'xxxxxxxxxxxxxxxxxxxxxx']));
$res = $client->request(
'GET',
$url.'/getMemberAccount',
['auth' => [$username,$api_token]]
);
$jsonData = json_decode($res->getBody(), true);
echo "<pre>";
print_r($jsonData);
echo "</pre>";

How to comunicate with an API with GuzzleHttp

I'm trying to communicate to an API with laravel using guzzleHttp but I can't receive nothing.
If I test in postman returns me information.
In postman I set the configuration as you see in image:
In code I'm trying to do that:
$token = env('API_TOKEN');
$company = env('API_COMPANY_ID');
$link = 'https://xpto.pt/APIs.aspx/Con';
$data = array('token' => $token, 'empresa' => $company);
$data = json_encode($data);
echo $data;
$client = new Client();
$request = $client->post($link, ['raw'=>$data]);
$response = $request->send();
dd($response);
Thank you

Strava Api issue

I'm using this composer package https://github.com/basvandorst/StravaPHP
The OAUTH is working fine and its generating me the JSON with the access token and user id, etc..
But whenever I try to use other function it returns me 404 not found.
Output is this :
{"token_type":"Bearer","access_token":"077058e0c800881c72a4b10a04a520d5898d4e3e","athlete":{"id":35670467,"username":"amir_do","resource_state":2,"firstname":"Amir","lastname":"Do","city":null,"state":null,"country":null,"sex":"M","premium":false,"summit":false,"created_at":"2018-10-13T13:55:41Z","updated_at":"2018-10-13T13:56:25Z","badge_type_id":0,"profile_medium":"https://lh5.googleusercontent.com/-ku6v9lKNgYY/AAAAAAAAAAI/AAAAAAAAAAA/ABtNlbASj8KhClhwnVYVqRrEG2oiYzWPbA/mo/photo.jpg","profile":"https://lh5.googleusercontent.com/-ku6v9lKNgYY/AAAAAAAAAAI/AAAAAAAAAAA/ABtNlbASj8KhClhwnVYVqRrEG2oiYzWPbA/mo/photo.jpg","friend":null,"follower":null,"email":"goncalomaia97#gmail.com"}}
35670467
Client error: `GET https://www.strava.com/api/athletes/35670467/stats?access_token=077058e0c800881c72a4b10a04a520d5898d4e3e` resulted in a `404 Not Found` response: {"message":"Record Not Found","errors":[{"resource":"resource","field":"path","code":"invalid"}]}
And this is my current callback.php page code:
<?php
include 'vendor/autoload.php';
use Strava\API\Client;
use Strava\API\Exception;
use Strava\API\Service\REST;
session_start();
$client = new GuzzleHttp\Client();
global $connect;
require_once("configs/database.php");
$connect = new mysqli($config['database']['host'],$config['database']['user'],$config['database']['pass'],$config['database']['db']);
$code = $_GET['code'];
$state = $_GET['state'];
$scope = $_GET['scope'];
$user = $_SESSION['username'];
$check = $connect->query("SELECT * FROM users WHERE email = '$user'");
$fetch = $check->fetch_array(MYSQLI_ASSOC);
$apix = $fetch['api'];
$api_secretx = $fetch['api_secret'];
$client = new GuzzleHttp\Client();
$data = [
"client_id" => $apix,
"client_secret" => $api_secretx,
"code" => $code
];
$result = $client->post('https://www.strava.com/oauth/token', ['json' => $data]);
print "<pre>";
print_r( $result->getBody()->getContents() );
print "</pre>";
$bodyb = $result->getBody();
$varx = json_decode((string) $bodyb, true);
$token = $varx['access_token'];
$id = $varx['athlete']['id'];
$_SESSION['token'] = $token;
printf($id);
try {
// REST adapter (We use `Guzzle` in this project)
$adapter = new \GuzzleHttp\Client(['base_uri' => 'https://www.strava.com/api/v3']);
// Service to use (Service\Stub is also available for test purposes)
$service = new \Strava\API\Service\REST($token, $adapter);
// Receive the athlete!
$client = new Client($service);
$athlete = $client->getAthleteStats($id);
print_r($athlete);
} catch(Exception $e) {
print $e->getMessage();
}
$adapter = new \GuzzleHttp\Client(['base_uri' => 'https://www.strava.com/api/v3']);
Change that to this (It should be trailing) :
$adapter = new \GuzzleHttp\Client(['base_uri' => 'https://www.strava.com/api/v3/']);

Guzzle Sends HTTP request to Google API, server 500 error

I use Guzzle to send HTTP request to Gmail API.
This part keeps causing a server 500 error, why is that ?
$data = new stdClass;
$data-> 'topicName' ='projects/sample.com:sample/topics/topic';
$data-> 'labelIds' = ["INBOX"];
$data-> 'labelFilterAction' = 'include';
Full code:
require_once __DIR__.'/vendor/autoload.php';
$client = new Google_Client();
$client->setScopes("https://www.googleapis.com/auth/gmail.readonly");
putenv('GOOGLE_APPLICATION_CREDENTIALS=sample.json');
$client->useApplicationDefaultCredentials();
// returns a Guzzle HTTP Client
$httpClient = $client->authorize();
$data = new stdClass;
$data->'topicName' ='projects/sample.com:sample/topics/topic';
$data-> 'labelIds' = ["INBOX"];
$data-> 'labelFilterAction' = 'include';
$request = new GuzzleHttp\Psr7\Request('POST', 'https://www.googleapis.com/gmail/v1/users/post#sample.com/watch',['Content-type'=>'application/json'],$data);
$response = $httpClient->send($request);
var_dump($response);
You should not have single quotes around you $data object's properties.
It should be:
$data->topicName ='projects/sample.com:sample/topics/topic';
$data->labelIds = ["INBOX"];
$data->labelFilterAction = 'include';

Google Cloud DNS Services - entity.change parameter is required but missing

With the following code using Google's PHP API Client I am receiving this response.
Google_Service_Exception with message 'Error calling POST https://www.googleapis.com/dns/v1/projects/PROJECT-NAME/managedZones/DNSZONE/changes: (400) The 'entity.change' parameter is required but was missing.
Where PROJECT-NAME and DNSZONE are my project and zone.
$client_email = MYCLIENT;
$private_key = file_get_contents('config/credentials/KEY.p12');
$scopes = array('https://www.googleapis.com/auth/ndev.clouddns.readwrite');
$project = "PROJECT-NAME";
$managedZone = "DNSZONE";
$creds = new Google_Auth_AssertionCredentials($client_email,$scopes,$private_key);
$client = new Google_Client();
$client->setAssertionCredentials($creds);
$resource = new Google_Service_Dns_ResourceRecordSet();
$resource->kind = "dns#resourceRecordSet";
$resource->name = "testing.DNSZONE.net.";
$resource->rrdatas[] = "testing.otherhost.com.";
$resource->ttl = 800;
$resource->type = "CNAME";
$dns = new Google_Service_Dns($client);
$change = new Google_Service_Dns_Change();
$change->kind = "dns#change";
$change->setAdditions($resource);
$dns->changes->create($project,$managedZone,$change);
I am a bit confused as to how to set this parameter. Or where I am even am to define it.
Just for clarify what the answer is, setAdditions expects an array.
$change->setAdditions([ $resource ]);

Categories