Request graphql with php - php

I need to make a graphql query using pure php, is this possible and how to do it?
graphql request:
{
getLastLowestProductPrices(
lastLowestPricesInput: [{date: "2023-01-01", productId: 14397, sizeId: "uniw"}]
) {
id
sizes {
id
price {
gross {
value
currency
}
}
}
}
endpoint: https://trening-dev1.iai-shop.com/graphql/v1/?docLang=pl
Please help me
Edit:
my php code:
function do_post_request($url, $data)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data)
)
);
return curl_exec($ch);
}
function get($endpoint)
{
$json = '{
getLastLowestProductPrices(
lastLowestPricesInput: [{date: "2023-01-01", productId: 14397, sizeId: "uniw"}]
) {
id
sizes {
id
price {
gross {
value
currency
}
}
}
}';
$result = do_post_request($endpoint, $json);
print_r($result);
}
$endpoint = 'https://trening-dev1.iai-shop.com/graphql/v1/?docLang=pl';
get($endpoint);

Related

How to pass string variable from dart to php file

I'm trying to send string variable to php file with post method this is my function in dart:
Future<Verify> verifyOTP() async {
var response_verifyotp = await http.post(Uri.parse(linkverifyOTP), body: {
"OTP_code": SOTP,
});
if (response_verifyotp.statusCode == 200) {
print(response_verifyotp.body);
st_verfiy = Verify.fromJson(json.decode(response_verifyotp.body));
print(st_verfiy.status);
}
}
but it doesn't pass correctly still appear to me it's missing value
I'm try also with: "OTP_code": SOTP.toString(),
it's same
This is my php code:
<?php
session_start();
$ch = curl_init();
$OTP_code=$_POST["OTP_code"];
if(isset($_SESSION["id"]))
$id=$_SESSION["id"];
curl_setopt($ch, CURLOPT_URL, "https://www.msegat.com/gw/verifyOTPCode.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
$fields = <<<EOT
{
"lang":"EN",
"userName": "Bloom_Ducks",
"apiKey":"f92b62af08ed0be",
"code":"$OTP_code",
"id": "$id" ,
"userSender":"APP"
}
EOT;
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json"
));
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$response=json_decode($response,true);
$code=$response["code"];
if($code == 1){
json_encode(array("status" => 1));
}else{
json_encode(array("status" => 0));
}
?>

need output to txt file instead of json object

Is this code convertible to print it's results to a txt file, instead of to a JSON / curl object? We are trying to debug a specific sku and we want to see the output of the long query which precedes this snippet:
foreach($group_sets AS $group_set) {
$bulk_json .= '{ "index" : { "_id" : "'.$group_set['our_sku'].'" } }'.PHP_EOL;
$bulk_json .= json_encode($group_set).PHP_EOL;
}
foreach($remove_skus AS $sku) {
$bulk_json .= '{ "delete" : { "_id" : "'.$sku.'" } }'.PHP_EOL;
}
print "processing batch, batch count: ".$batch_cnt.PHP_EOL;
send_to_elastic($bulk_json);
$bulk_json = "";
$batch_cnt = 0;
$batch_sku_list = array();
}
}
if(!empty($bulk_json)) {
send_to_elastic($bulk_json);
$bulk_json = "";
}
print PHP_EOL.PHP_EOL."DONE".PHP_EOL.PHP_EOL;
function send_to_elastic($bulk_json) {
$url = "https://ada64ff1913a4b.us-east-1.aws.found.io:9243/us/product/_bulk";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERPWD, "MyName:MyPassword");
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $bulk_json);
echo "uploading batch to elastic-cloud... ";
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$response = json_decode($json_response, true);
//if ( $status != 201 ) {
// print("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
//}
curl_close($curl);
echo "done".PHP_EOL;
Here is my modified code, it doesnt write to the file though so obviously I have something screwed up...
foreach($group_sets AS $group_set) {
$bulk_json .= '{ "index" : { "_id" : "'.$group_set['my_sku'].'" } }'.PHP_EOL;
$bulk_json .= json_encode($group_set).PHP_EOL;
}
foreach($remove_skus AS $sku) {
$bulk_json .= '{ "delete" : { "_id" : "'.$sku.'" } }'.PHP_EOL;
}
print "processing batch, batch count: ".$batch_cnt.PHP_EOL;
//send_to_elastic($bulk_json);
file_put_contents('searchresults.txt', $bulk_json);
$bulk_json = "";
$batch_cnt = 0;
$batch_sku_list = array();
}
}
if(!empty($bulk_json)) {
//send_to_elastic($bulk_json);
file_put_contents('searchresults.txt', $bulk_json);
$bulk_json = "";
}
print PHP_EOL.PHP_EOL."DONE".PHP_EOL.PHP_EOL;
//function send_to_elastic($bulk_json) {
//$url = "https://ada64ff1913a4b16us-east-1.aws.found.io:9243/qm/product/_bulk";
//$curl = curl_init($url);
//curl_setopt($curl, CURLOPT_HEADER, false);
//curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($curl, CURLOPT_USERPWD, "MyUser:MyPW");
//curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
//curl_setopt($curl, CURLOPT_POST, true);
//curl_setopt($curl, CURLOPT_POSTFIELDS, $bulk_json);
//echo "uploading batch to elastic-cloud... ";
//$json_response = curl_exec($curl);
//$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
//$response = json_decode($json_response, true);
//if ( $status != 201 ) {
// print("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
//}
//curl_close($curl);
echo "done".PHP_EOL;
Use file_put_contents(). Replace the call to send_to_elastic() with:
file_put_contents('filename.txt', $bulk_json);
You should also generate the JSON correctly, something like:
$result = array();
foreach($group_sets AS $group_set) {
$result[] = ["index" => [ "_id" => $group_set['our_sku']]];
$result[] = $group_set;
}
foreach($remove_skus AS $sku) {
$result[] = [ "delete" => [ "_id" => $sku ]];
}
$bulk_json = json_encode($result);

Fetching orders details throught flipkart api using PHP(how to recall function without rewrite function declaration)

I am fetching order details through Flipkar order Api.It is gives first 20(0-20) result at a time and for next records it gives next page url.
For fetching next 20 records(20-40) again we have to call curl with next page url and fetch orders .For this i am using code below:
$listingbulk=array();
$headers = array(
'Cache-Control: no-cache',
'Content-type: application/json',
'Authorization: Bearer '.$fkt
);
$bulkjson= '{
"filter": {
"orderDate": {
"fromDate": "'.$orderfrom.'",
"toDate": "'.$orderto.'"
}
}
}';
$urlbulk = "https://api.flipkart.net/sellers/v2/orders/search";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$urlbulk);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $bulkjson);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$resultbulksku = curl_exec($curl);
$listingbulk[] = json_decode($resultbulksku);
if (curl_errno($curl)) {
echo 'Error:' . curl_error($curl);
}
curl_close ($curl);
$nextPageUrl= $listingbulk[0]->nextPageUrl;
if ($nextPageUrl !=''){
$newpageurl= orderFk($nextPageUrl,$headers);
if ($newpageurl !='') {
$newpageurl2= orderFk($newpageurl,$headers);
if ($newpageurl2 !=''){
$newpageurl3= orderFk($newpageurl2,$headers);
}
}
}
Function is here :
function orderFk($nextPageUrl,$headers){
$fp = fopen('order/order'.$currenttime.'.csv',"a");
$urlbulk1 = "https://api.flipkart.net/sellers/v2/".$nextPageUrl;
$curl1 = curl_init();
curl_setopt($curl1, CURLOPT_URL,$urlbulk1);
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl1, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl1, CURLOPT_HTTPHEADER, $headers);
$resultbulksku1 = curl_exec($curl1);
$listingbulk1[] = json_decode($resultbulksku1);
if (curl_errno($curl1)) {
echo 'Error:' . curl_error($curl1);
}
curl_close ($curl1);
$listingbulk=$listingbulk1;
$newnextPageUrl= $listingbulk1[0]->nextPageUrl;
return $listingbulk;
}
I want to crate if else condition dynamically so if "next pageurl" is exist in response it should call again the same function with new url ( without calling function multiple times in if else condition).
If anyone has solution for this please reply.(answer with working example would be more helpful)
I suggest something like this, inside order function use while loop, exit on error or loop exceed the maxpage. In $list u have all responses.
function orderFk($nextPageUrl,$headers){
$page = 1;
$maxpage = 100;
$exit = 0;
$list = array();
while(strlen($nextPageUrl)){
$urlbulk = "https://api.flipkart.net/sellers/v2/".$nextPageUrl;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$urlbulk);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$resultbulk = curl_exec($curl);
$nextPageUrl = '';
if(curl_errno($curl)) {
$exit = 1;
}
curl_close($curl); // close before exit while loop
if($exit==1 || $page>$maxpage){
break;
}
$listingbulk = json_decode($resultbulk);
if(strlen($listingbulk[0]->nextPageUrl)>0){
$nextPageUrl = $listingbulk[0]->nextPageUrl;
}
$list[] = $listingbulk;
$page++;
}
return $list;
}

Website form data stop saving into Google spreadsheet from yesterday i.e 26/05/2015

I have created an application where my website form data will be stored into a google spreadsheet. It was working fine till yesterday i.e 26/05/2015. But from today i.e 27/05/2015 it suddenly stopped working. And no value is adding into the google spreadsheet.
I have used the below mentioned class for spreadsheet application
spreadsheet.php:
<?php
class spreadsheet {
private $token;
private $spreadsheet;
private $worksheet;
private $spreadsheetid;
private $worksheetid;
public function __construct() {
}
public function authenticate($username, $password) {
$url = "https://www.google.com/accounts/ClientLogin";
$fields = array(
"accountType" => "HOSTED_OR_GOOGLE",
"Email" => $username,
"Passwd" => $password,
"service" => "wise",
"source" => "pfbc"
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if($status == 200) {
if(stripos($response, "auth=") !== false) {
preg_match("/auth=([a-z0-9_\-]+)/i", $response, $matches);
$this->token = $matches[1];
}
}
}
public function setSpreadsheet($title) {
$this->spreadsheet = $title;
}
public function setWorksheet($title) {
$this->worksheet = $title;
}
public function add($data) {
if(!empty($this->token)) {
$url = $this->getPostUrl();
if(!empty($url)) {
$headers = array(
"Content-Type: application/atom+xml",
"Authorization: GoogleLogin auth=" . $this->token,
"GData-Version: 3.0"
);
$columnIDs = $this->getColumnIDs();
if($columnIDs) {
$fields = '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">';
foreach($data as $key => $value) {
$key = $this->formatColumnID($key);
if(in_array($key, $columnIDs))
$fields .= "<gsx:$key><![CDATA[$value]]></gsx:$key>";
}
$fields .= '</entry>';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
}
}
}
}
private function getColumnIDs() {
$url = "https://spreadsheets.google.com/feeds/cells/" . $this->spreadsheetid . "/" . $this->worksheetid . "/private/full?max-row=1";
$headers = array(
"Authorization: GoogleLogin auth=" . $this->token,
"GData-Version: 3.0"
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if($status == 200) {
$columnIDs = array();
$xml = simplexml_load_string($response);
if($xml->entry) {
$columnSize = sizeof($xml->entry);
for($c = 0; $c < $columnSize; ++$c)
$columnIDs[] = $this->formatColumnID($xml->entry[$c]->content);
}
return $columnIDs;
}
return "";
}
private function getPostUrl() {
$url = "https://spreadsheets.google.com/feeds/spreadsheets/private/full?title=" . urlencode($this->spreadsheet);
$headers = array(
"Authorization: GoogleLogin auth=" . $this->token,
"GData-Version: 3.0"
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if($status == 200) {
$spreadsheetXml = simplexml_load_string($response);
if($spreadsheetXml->entry) {
$this->spreadsheetid = basename(trim($spreadsheetXml->entry[0]->id));
$url = "https://spreadsheets.google.com/feeds/worksheets/" . $this->spreadsheetid . "/private/full";
if(!empty($this->worksheet))
$url .= "?title=" . $this->worksheet;
curl_setopt($curl, CURLOPT_URL, $url);
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if($status == 200) {
$worksheetXml = simplexml_load_string($response);
if($worksheetXml->entry)
$this->worksheetid = basename(trim($worksheetXml->entry[0]->id));
}
}
}
curl_close($curl);
if(!empty($this->spreadsheetid) && !empty($this->worksheetid))
return "https://spreadsheets.google.com/feeds/list/" . $this->spreadsheetid . "/" . $this->worksheetid . "/private/full";
return "";
}
private function formatColumnID($val) {
return preg_replace("/[^a-zA-Z0-9.-]/", "", strtolower($val));
}
}
?>
and use it in following way:
include 'spreadsheet.php';
$doc = new spreadsheet();
$doc->authenticate("example#example.com", "example");
$doc->setSpreadsheet("Tester");
$doc->setWorksheet("Sheet1");
$my_data = array("First Name" => "John", "Last Name" => "Doe");
$doc->add($my_data);
Please help me out. Thanks in advance.
The ClientLogin method, which has been deprecated for a number of years, has now been turned off. It will return a 404 when trying to authenticate. You will need to migrate to OAuth for authentication.
See https://developers.google.com/identity/protocols/AuthForInstalledApps
Here is an updated PHP library that uses OAuth - https://github.com/asimlqt/php-google-spreadsheet-client. The author also has a sample project with instructions to generate OAuth access tokens.

Yammer - How to post via api to activity stream? (php)

I am able to "GET" the activity stream but not "POST" to it. It seems its a technical error.
This is the code which works for getting the activity stream:
function getActivityStream()
{
$as=$this->request('https://www.yammer.com/api/v1/streams/activities.json');
var_dump($as);
}
function request($url, $data = array())
{
if (empty($this->oatoken)) $this->getAccessToken();
$headers = array();
$headers[] = "Authorization: Bearer " . $this->oatoken['token'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($data) );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
return json_decode($output);
}
ok so that works fine...
the code below, returns a Yammer "oops this page could not be found" message:
function putActivityStream()
{
$data=array('type'=>'text', 'text'=>'hello from api test call');
$json=json_encode($data);
$res=$this->post('streams/activites.json',$json);
}
function post($resource, $data)
{
if (empty($this->oatoken)) $this->getAccessToken();
$ch = curl_init();
$headers = array();
$headers[] = "Authorization: Bearer " . $this->oatoken['token'];
$headers[]='Content-Type: application/json';
$url = 'https://www.yammer.com/api/v1/' . $resource;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
return $response;
}
One of the examples from:
http://developer.yammer.com/api/streams.html
POST https://www.yammer.com/api/v1/streams/activities.json
Requests must be content-type: application/json.
{
"type": "text",
"text": "The build is broken."
}
You're going to kick yourself. You have a typo in your code. :)
Change:
$res=$this->post('streams/activites.json',$json);
to
$res=$this->post('streams/activities.json',$json);
Simples.

Categories