the response is coming null in curl [duplicate] - php

This question already has an answer here:
Curl error: Could not resolve host: www.localhost
(1 answer)
Closed 4 years ago.
the response is coming as NULL .Here is my code:
$url = "http://www.localhost:81/dbWIP/selectApi.php?name='$name'";
$client = curl_init();
echo $client;
curl_setopt($client, CURLOPT_URL,$url);
curl_setopt($client,CURLOPT_RETURNTRANSFER,true);
curl_setopt($client, CURLOPT_HEADER, false);
$response = curl_exec($client);
curl_close($client);
$result = json_decode($response);
echo $response;

The $name var isn't properly showing try this:
$url = "http://www.localhost:81/dbWIP/selectApi.php?name=".$name;
Right now it is just showing:
$url = "http://www.localhost:81/dbWIP/selectApi.php?name='$name'";

Related

php access array from POST response [duplicate]

This question already has answers here:
Get JSON object from URL
(11 answers)
Closed 11 months ago.
i use a php post to get the number of users in the telegram group
<?php
$post = array(
'chat_id'=>$chat_id);
$ch = curl_init("https://api.telegram.org/bot$tokken/getChatMembersCount?");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING,"");
header('Content-Type: text/html');
$posres = curl_exec($ch);
$data1 = json_encode($posres);
echo $data1;
?>
the response are array :
{"ok":true,"result":3}
how can i access the result value i tried echo $data1[19]; this only give the index position value
thank you
The response is JSON, so you need to use json_decode() to convert it to a PHP array or object.
$data1 = json_decode($posres);
echo $data1->result;
Just in case Your response is array of object
echo $data1['19']->result; this should give 3 according to your question

PHP Curl - Correct URL Parameters [duplicate]

This question already has answers here:
PHP, cURL, and HTTP POST example?
(14 answers)
Closed 3 years ago.
I just played with PHP Curl for the first time and wrote a sample code, but I don't know how to pass the parameters in the URL correctly.
Remote Site Code:
if(isset($_REQUEST['updateme']))
{
$license = $Gauntlet->filter($_REQUEST['licensekey']);
$domainnew = $Gauntlet->filter($_REQUEST['domainnew']);
$domainold = strtolower($_SERVER['REMOTE_ADDR']);
$sqlcheckexist = "SELECT * FROM licenses WHERE licensekey = '$license' AND host = '$domainold' AND status = 'active'";
$querycheck = $DatabaseHandler->query($sqlcheckexist);
if($querycheck)
{
if($querycheck->num_rows < 1){
$json['status'] = 301;
$json['message'] = 'Wrong Domain or License';
}else{
$sqlupdate = "UPDATE licenses SET host = '$domainnew', status = 'active' WHERE licensekey = '$license'";
$json['message'] = 'Success!';
}
}
}
Remote Site API Url: mydomain.com/myapi/
How can i do this REQUEST in Url with parameters if i POST licensekey and domainnew and Update my Database value?
You can post value using php curl like this
$payload=array();
$payload['licensekey']='abc';
$payload['domainnew']='123';
$payload['updateme']='update';
$payload=json_encode($payload);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_POSTREDIR, 3);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res=curl_exec($ch);

Running shell commands inside curl call in php [duplicate]

This question already has answers here:
How can I debug exec() problems?
(4 answers)
sudo in php exec()
(8 answers)
How can I get useful error messages in PHP?
(41 answers)
PHP - Debugging Curl
(8 answers)
Closed 3 years ago.
I am using curl as like below.
http://99.88.77.121/sample/curl.php
$ch = curl_init();
$ip = "Some IP address";
curl_setopt($ch, CURLOPT_URL, 'http://99.88.77.121/test/index.php');
curl_setopt($ch, CURLOPT_POST, 1);
$payload = json_encode(
array(
'username' => 'abc',
'password' => '1234'
)
);
curl_setopt($ch, CURLOPT_POSTFIELDS,$payload);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array("REMOTE_ADDR: $ip", "HTTP_X_FORWARDED_FOR: $ip"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec ($ch);
The above code sends the data to the Url.
I have code like below in my http://99.88.77.121/test/index.php
<?php
$fp = fopen('php://input', 'r');
$rawData = stream_get_contents($fp);
$data = json_decode($rawData);
$username = $data->username;
$password = $data->password;
$projectName = "testFolder";
chdir('../');
//chdir($projectName);
$command = "sudo chmod -R 777 ".$projectName;
shell_exec($command);
?>
I am trying give full permission for testFolder using above code,
is chdir() and shel_exec() will work here? In my case its not working
If i run the index.php in browser, its working fine. but not from the curl call.
Can anyone look into it and update me your thoughts please.

Array navigation in PHP [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I am trying to get an api(steam api) response as a variable.
{"response":{"players":[{"steamid":"XXXXXXXXXXXXXXXXXXXX","communityvisibilitystate":3,"profilestate":1,"personaname":"The value I want to get","lastlogoff":XXXXXXXXXX,"profileurl":"XXX","avatar":"X","avatarmedium":"X","avatarfull":"X","personastate":1,"realname":"X","primaryclanid":"X","timecreated":XXXXXXXXXX,"personastateflags":0}]}}
Formatted: https://pastebin.com/8QtLzwVX
Currently I am using the following code to get the array:
$url="http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&steamids=" . $sid;
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
I don't know how to get the personaname value from the JSON I get in $result and put it in a variable like $personaname.
try with this one:
<?php
$json_array = json_decode($result, true);
echo $json_array['response']['players'][0]['personaname'];

Laravel/PHP - How to check if JSON is empty? [duplicate]

This question already has answers here:
How to check if JSON object is empty in PHP?
(5 answers)
Closed 6 years ago.
This may sound DUPLICATE, but I tried all the answers with this same question.
I am using this API, https://smartystreets.com/docs/cloud/us-street-api
My problem is, the output of their INVALID ADDRESS is
[ ]
It is also stated in their documentation.
Now, this is my code but it's not working.
$url = 'SOME_API_HERE' ;
$url = str_replace(" ", '%20', $url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$json = json_decode($result, true);
if (!empty($json))
{
return json_decode($result, true);
}
else {
return 'invalid address';
}
Convert JSON to an array first and then check if this array is empty or not:
$data = json_decode($result, true);
return empty($data) ? 'invalid address' : $data;

Categories