how to make a get call with JSON and retrieve data - php

I am trying to follow instructions from an RFC spec to retrieve data from "certificate transparency log" known logs here
Instructions say to make a JSON call but i don't seem to be having any success.. i actually don't get any data response at all.
Here is the guide I am trying to follow:
https://www.rfc-editor.org/rfc/rfc6962#section-4.6
Here is the php code I am using
<?php
$data = array("start" => "1", "end" => "10");
$data_string = json_encode($data);
$ch = curl_init('https://ct.googleapis.com/pilot/ct/v1/get-entries');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
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))
);
$result = curl_exec($ch);
var_dump(json_decode($result, true));
?>

The service I was connecting to has an SSL certificate installed and was not responding to my requests. Disabling the trust verification was the solution. I added this to the CURL options CURLOPT_SSL_VERIFYPEER => false

Related

INVALID_TICKET in Zoho mail php Integration

I am using zoho email for sending email.
but when Trying the following code to send email ,
it returns the error invalid_ticket.
$data = array(
"fromAddress"=> "from#gmail.com",
"toAddress"=> "receive#gmail.com",
"subject"=> "Email - Always and Forever",
"content"=> "Email can never be dead. The most neutral and effective way, that can be used for one to many and two way communication.",
"askReceipt" => "yes"
);
// Convert the PHP array into a JSON format
$payload = json_encode($data);
// Initialise new cURL session
$ch = curl_init('https://mail.zoho.in/api/accounts/<user_id>/messages');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($payload))
);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
?>
Please help me to solve this. Thanks
This is not the right forum for this question but according to official documentation, you need to Kindly regenerate the authtoken with that generated authtoken which will work fine.
Source: https://help.zoho.com/portal/en/community/topic/zoho-cretor-getting-error-message-invalid-ticket-id-code-4834-when-trying-to-insert-records-in-zoho-crm

Twitter API token request returning gobbledygook

I'm trying to use Application Only Authentication, as described here:
https://developer.twitter.com/en/docs/basics/authentication/overview/application-only
I'm using the following PHP code to do so.
if(empty($_COOKIE['twitter_auth'])) {
require '../../social_audit_config/twitter_config.php';
$encoded_key = urlencode($api_key);
$encoded_secret = urlencode($api_secret);
$credentials = $encoded_key.":".$encoded_secret;
$encoded_credentials = base64_encode($credentials);
$request_headers = array(
'Host: api.twitter.com',
'User-Agent: BF Sharing Report',
'Authorization: Basic '.$encoded_credentials,
'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
'Content-Length: 29',
'Accept-Encoding: gzip'
);
print_r($request_headers);
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, 'https://api.twitter.com/oauth2/token');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
$attempt_auth = curl_exec($ch);
print_r($attempt_auth);
}
It should return JSON with the token in it, but instead it returns gobbledygook, as seen in the image below:
I'm sure I'm missing some very simple step, where am I going wrong?
If I send the curl request without the headers, it returns an error in JSON format as expected, so is there something wrong with my headers?
You have few options here. Instead of setting header directly, use below
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
If you set header directly then you should use
print_r(gzdecode($attempt_auth));
See below thread as well
Decode gzipped web page retrieved via cURL in PHP
php - Get compressed contents using cURL

discord php curl login Fail

i want to login into discord with php curl but did't work's for me what is the problem in my code i want auto-login into discord and grab data from html ....!
cloed
There are some advices:
1) If you want to see some responsed information. You have to echo $answer. Otherwise you couldn't see the response even the data were sent correctly.
2) The site used captcha. I tried it with correct email/password and get the following response {"captcha_key": ["captcha-required"]} . I have no idea how to round this. Maybe you should contact the Discord.
3) The site requires json data. Here are the codes I used:
<?php
$data = array("email" => "XXXX", "password" => "XXXX");
$data_string = json_encode($data);
$ch = curl_init('https://discordapp.com/api/v6/auth/login');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
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))
);
$answer = curl_exec($ch);
echo $answer;
if (curl_error($ch)) {
echo curl_error($ch);
}
?>
Hope this will help.

Api.ai API for php (with REST API by POSTing)

llo,
I'm trying to integrate my webapp with RESTful api in php. This requires me to POST data to https://api.api.ai/v1/query and also have 2 custom headers.
my current code is
$data = array("v" => "20150821", "query" => "whats the time", "lang" => "EN", "sessionId" => "1234567890");
$data_string = json_encode($data);
$ch = curl_init('https://api.api.ai/v1/query');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
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),
'Content-type: application/json',
'Authorization: Bearer XxxX-xxx',
'ocp-apim-subscription-key: XxxX-xxx')
);
$result = curl_exec($ch);
If i run this, (with my real auth and key obv) i get nothing. Like Blank. Any ideas?
I AM SO SORRY.
I havent been sleeping well and did a rather idiotic mistake
i forgot to echo $result
i know, i know. I'll have to be a bit more careful
Thanks anyway :P

Send and Receive JSON using an https curl_exec call in PHP

I'm trying to make a curl call using PHP. My project has a flow like this
I redirect user to the server.
User uses the services and the server redirects him to a page on my application.
I would like to make a server to server call at this point to verify certain details.
Point to be noted - I post JSON data as well as receive JSON data as response.
I have tried this. I am able to send data but don't get any response.
Is this correct?
Please help!
$data = array("One" => "Onedata", "Two" => "Twodata");
$JsonData = json_encode($data);
$ch = curl_init('https://exampleurl');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $JsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($JsonData))
);
$result = curl_exec($ch);
As you mention in the comment:
The problem is with SSL verification of the host. It takes some effort (see http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/) to setup SSL correctly, so a workaround is to add the following two options:
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
Try to use the below code which works for me. and make sure the json_url is correct. When you run it on the browser you should get an output. Before try it using CURL, run in on the browser and see whether you get the output you want.
$json_url ='https://exampleurl';
// Initializing curl
$ch = curl_init( $json_url );
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json')
);
// Setting curl options
curl_setopt_array( $ch, $options );
// Getting results
$results = curl_exec($ch); // Getting jSON result string
$json_decoded = json_decode($results);

Categories