How to Post to googleapis civicinfo using PHP to retrieve JSON data - php

I'm trying to retrieve CivicInfo data from Google APIs using following PHP code:
$address = $_GET["address"];
$api_key = "[my key]";
$url = "https://civicinfo.googleapis.com/civicinfo/v2/representatives?address=" . $address . "&key=" . $api_key;
$payload = json_encode(array('address' => $address));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result, TRUE);
My code above fails, but I'm not seeing any error. When I do echo $result['status'], nothing is displayed.
Please help!

Related

Send Mailchimp campaign to a specific email address via API v3

I have got the following code but I'm getting a resource not found error.
Has anyone successfully sent an MC campaign to a single user?
$apiKey = 'APIKEYHERE';
$campaignId = 'CAMPAIGNIDHERE';
$memberId = md5(strtolower("test#example.com"));
$dataCenter = 'us19';
$url = 'https://'. $dataCenter . '.api.mailchimp.com/3.0/campaigns/' . $campaignId .'/actions/test';
$jsonEmail = '{}';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'apikey:'.$apiKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonEmail);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);

Post json data with PHPCuRL Raw data

$data = array('msisdn' =>'01033000939','basic'=>true);
$data_json = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->urlFullSubs);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_json)));
$response = curl_exec($ch);
$s = curl_error($ch);
curl_close($ch);
return $response;
Hi I want to get information from the API and i POST Json data using PHP Curl but I cannot get the output.Can someone let me know where did I do wrong

how to get address, contact details from url/web site using bing api?

can anybody help me.
I have some urls/web sites(like www.facebook.com, www.olx.com, www.abc.com.... etc). From the urls i need to get address/contact details using "Bing" api.
I have an api
https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=...........
by this im getting website and few details but not showing address and contact details.
this is my code
$accountKey = 'XXXX';
$ServiceRootURL = 'https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/';
$sitehost = 'facebook';
$WebSearchURL = $ServiceRootURL . 'Web?$format=json&Query=';
$request = $WebSearchURL . urlencode( '\'' . $sitehost . '\'');
$response = file_get_contents_curl($request,$accountKey,$accountKey);
$jsonobj = json_decode($response,true);
function file_get_contents_curl($url,$user,$pass)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, $user.':'.$pass);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
return $data;
}
If you have any solution, please let me know
thank you in advance.

Don't show logs with PHP command and cURL

I developed a script with PHP which I use with the console, with the command php index.php.
In this script, I use cURL to query a server.
The problem is my script shows logs on the console, and I just want the result of the echo.
Do you have an idea to hide these logs?
// GET TOKEN
// /////////////////////////////////////////////////////////////////////////////
$url = "some/url";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pwd");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$data = curl_exec($ch);
$location = "";
preg_match_all('/^Location:(.*)$/mi', $data, $location);
$location = trim($location[1][0]);
$location = parse_url($location);
parse_str($location['query'], $attrs);
$token = $attrs['code'];
if( isset($token) ) {
// GET CONNEXION
// /////////////////////////////////////////////////////////////////////////
$url = "some/url";
$post_data = ['code' => $token, 'grant_type' => 'authorization_code'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$client_id:$client_secret");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = json_decode(curl_exec($ch));
$connexion = isset($data->access_token) ? $data->access_token : $data->error_description;
// GET LOGIN
// /////////////////////////////////////////////////////////////////////////
$url = "some/url";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
$data = json_decode($data);
// $login = json_encode(['public' => $data->api_keys[1]->public, 'secret' => $data->api_keys->secret]);
// REQUEST
// /////////////////////////////////////////////////////////////////////////
$url = $request . "?externalId=".$external_id."&externalSource=".$external_source;
date_default_timezone_set("Europe/Paris");
$nonce = generateRandomString();
file_put_contents('php://stderr', print_r("Set random nonce to " . $nonce . "\n", TRUE));
$created = date("Y-m-dTH:i:sP");
$created = date("Y-m-dTH:i:sP");
$username = $data->api->public;
$secret = $data->api->secret;
$pwd_digest = base64_encode(sha1($nonce.$created.$secret));
$auth_header = "X-WSSE: UsernameToken Username=\"$username\", PasswordDigest=\"$pwd_digest\", Nonce=\"$nonce\", Created=\"$created\"";
$header = array($auth_header, 'Accept: something', 'Accept-Language: en');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
$data = json_decode($data);
echo $data->nbNqPoints;
} else {
echo "0";
}
Answer found at this address: managing curl output in php
I add this line for every cURL instance :
curl_setopt($ch, CURLOPT_VERBOSE, 0);

Basecamp API using cURL and PHP

I'm trying to connect to the basecamp api (json) using PHP + cURL, but all my attempts have failed, and I'm not sure why. The end goal is to build a dashboard that displays information from basecamp projects to team members over http.
<?php
$basecamp_url = 'https://basecamp.com/xxxxxx/api/v1';
$username = 'username';
$password = 'pass';
$session = curl_init();
curl_setopt($session, CURLOPT_URL, $basecamp_url.'/projects.xml');
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_HTTPGET, 1);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session,CURLOPT_USERPWD,$username . ":" . $password);
$response = curl_exec($session);
curl_close($session);
echo '<pre>';
print_r($response);
?>
I am working on a very similar project as you are right now. You have to create a Basecamp app first. Than the following code should get you connected:
$appName = 'your app name';
$appContact = 'your app email;
$basecampAccountId = 'xxxxx';
$basecampUsername = 'youremailhere';
$basecampPassword = 'yourpassword here';
$baseUrl = "https://basecamp.com/$basecampAccountId/api/v1";
$url= $baseUrl.'/projects.json';
$credentials = "$basecampUsername:$basecampPassword";
$helloHeader = "User-Agent: $appName ($appContact)";
echo $url.'<br>';
echo $credentials.'<br>';
echo $helloHeader.'<br>';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, $credentials);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($helloHeader));
$response = curl_exec($ch);
$errno = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
print_r($response);

Categories