How could I use cURL to get back an array from this URL? http://module.game-monitor.com/67.202.102.136:27016/data/server.php
This is my code so far.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://module.game-monitor.com/67.202.102.136:27016/data/server.php');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false); // remove body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$head = unserialize($head);
$head = objectToArray($head);
$head["query_time"] = str_ireplace('ms', '', $head["query_time"]);
return $head;
Try just use PHP Type Casting (array) and remove the objectToArray function
$head = unserialize($head);
$head = (array) $head ;
var_dump($head);
Output
array
'ip' => string '67.202.102.136' (length=14)
'port' => string '27016' (length=5)
'player' => int 0
'maxplayer' => int 14
'name' => string 'Another www.cogameservers.com CSGO Server' (length=41)
'premium' => string '0' (length=1)
'link' => string 'http://www.game-monitor.com/csgo2_GameServer/67.202.102.136:27016/Another_www.cogameservers.com_CSGO_Server.html' (length=112)
'error' => int 0
'query_time' => string '0ms' (length=3)
Related
I am using an online laravel api documentation in my app. When i run my code in my browser, i get the error "pass phonenumber field as an array". But the number field is being already passed as an array field. What could i be missing below in my code ? Thanks in advance
public function testAPI(Request $request)
{
$on_call_back = 'https://learntoday.co.uk/var';
$id = '*****';
$url = $on_call_back.'?key='.$id;
$variables = [
'phoneNumber' => ['44234200234','44234242002'],
'from' => 'world',
'content' => 'I love to code',
];
$ch = curl_init();
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($variables));
$result = curl_exec($ch);
$result = json_decode($result, TRUE);
curl_close($ch);
}
When i return $variables, i get the response
Array
(
[phoneNumber] => Array
(
[0] => 44234200234
[1] => 44234242002
)
[from] => test
[content] => I love to code
)
{"status":"error","message":"Make sure you are passing the phoneNumber field as an array"}
Your syntax of the array seems to be wrong, have you tried putting each number into single quotes like this:
'phoneNumber' => ['44234200234', '44234242002'],
Change
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($variables));
to
curl_setopt($ch, CURLOPT_POSTFIELDS, $variables);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($variables));
You can also do curl_setopt($ch, CURLOPT_POST, 1); instead of CURLOPT_CUSTOMREQUEST.
Please try send $variable as below code
$variables = array(
'phoneNumber' => array('44234200234','44234242002'),
'from' => 'world',
'content' => 'I love to code',
);
You should try this:
$variables = [
'phoneNumber' => ['44234200234','44234242002'],
'from' => 'world',
'content' => 'I love to code',
];
Updated answer
public function testAPI(Request $request)
{
$on_call_back = 'https://learntoday.co.uk/var';
$id = '*****';
$url = $on_call_back.'?key='.$id;
$variables = [
'phoneNumber' => ['44234200234','44234242002'],
'from' => 'world',
'content' => 'I love to code',
];
$ch = curl_init();
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($variables));
$result = curl_exec($ch);
$result = json_decode($result, TRUE);
curl_close($ch);
}
I am using a online laravel api doc to execute a program. When i run my code in postman, i get the error "pass number field as an array". But the number field is being already passed as an array field. What could i be missing below in my code ? Thanks in advance
public function testAPI(Request $request)
{
$on_call_back = 'https://learntoday.co.uk/var';
$id = '*****';
$url = $on_call_back.'?key='.$id;
$variables = [
'number' => ['44234200234,44234242002'],
'from' => 'world',
'content' => 'I love to code',
];
$ch = curl_init();
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($variables));
$result = curl_exec($ch);
$result = json_decode($result, TRUE);
return $data;
curl_close($ch);
}
When i print out my results, this is what i see
Array
(
[number] => Array
(
[0] => '44234200234,44234242002'
)
[from] => world
[content] => i love to code
)
{"status":"error","message":"Pass number field as an array"}
Here is the update of how it appears now
Array
(
[recipient] => Array
(
[0] => 44234200234
[1] => 44234242002
)
[from] => world
[content] => i love to code
)
You should try this:
public function testAPI(Request $request)
{
$on_call_back = 'https://learntoday.co.uk/var';
$id = '*****';
$url = $on_call_back.'?key='.$id;
$variables = [
'number' => ['44234200234','44234242002'],
'from' => 'world',
'content' => 'I love to code',
];
$ch = curl_init();
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($variables));
$result = curl_exec($ch);
$result = json_decode($result, TRUE);
return $data;
curl_close($ch);
}
You should try passing numbers on different indexes of array:
$variables = [
'number' => ['44234200234','44234242002'],
'from' => 'world',
'content' => 'I love to code',
];
Currently you are passing both numbers separated with , on 0 index.
Try Like This :
$numbersArray = array("44234200234","44234242002");
$variables = [
//'number' => ['44234200234','44234242002'],
'number' => $numbersArray,
'from' => 'world',
'content' => 'I love to code',
];
Ideally Old solution should work but you can try like this and check it.
You are passing your array as 1 string. To achieve with this (incorrect) approach, you would need to seperate the strings by a comma, and then convert each string to an integer.
'number' => ['44234200234,44234242002,1,2,3,4'], // this is ONE string
'number' => ['1', '2', '3', '4'] // this is many strings
What you want to do is pass an array of integers
numbers => [1, 2, 3]
Happy coding :-)
Try to use serialize function:
$on_call_back = 'https://learntoday.co.uk/var';
$id = '*****';
$url = $on_call_back.'?key='.$id;
$variables = [
'number' => serialize(['44234200234','44234242002']),
'from' => 'world',
'content' => 'I love to code',
];
I am trying to access the json data from an API, and displaying it on the webpage for the time being. I am able to fetch the data array, but I want only 1 field in that data. Below is my code.
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'ACCEPT: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL,"https://www.localmed.com/providers/us_0651651651_9vg5u1r2g1tw3e5_f24c866/openings/?reasonForVisitId=us_1223_new_teethcleaning-adult");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);
curl_close($ch);
//$json_a=json_decode($result,true);
var_dump(json_decode($result, true));
var_dump ($result[1]['time']);
Below are the results,
I need to get only the 'time' field. please help. I am getting error undefined index 'time'
array (size=1)
'openings' =>
array (size=10)
0 =>
array (size=4)
'id' => string 'us_0651651651_9vg5u1r2g1tw3e5_f24c866_20160909T130000Z_67cbe00' (length=62)
'uri' => string '/providers/us_0651651651_9vg5u1r2g1tw3e5_f24c866/openings/us_0651651651_9vg5u1r2g1tw3e5_f24c866_20160909T130000Z_67cbe00/' (length=121)
'display' => null
'time' => string '2016-09-09T13:00:00Z' (length=20)
I used this code but I get always empty result I don't know why
if (isset($_POST['uname'], $_POST['password'])){
$uname = test_input($_POST['uname']);
$pw = test_input($_POST['password']);
$data = array(
"loginEmail" => $uname,
"loginPassword" => $pw,
"clientURI" => "https://hk2.notify.windows.com/?token=AwYAAAC2bng45wHDpXWajWLTxGM1gxP1DcLlHTL8%2bhOfKVbkpYjl1U9tnUGrW4FSmwuiiWPKEyvSUoO5v9nfzFWZ097kdUeN8xDpAlp96Ilk3LCSN0sQNFuyU%3d"
);
$url_send ="https://dev-frontserver.com/api/login";
$str_data = json_encode($data);
$headers = array("Content-Type: application/json");
function sendPostData($url, $post, $headers){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
echo "result = " .sendPostData($url_send, $str_data, $headers);*/
}
Any idea what is wrong with my code ? I tried the api in https://www.hurl.it and it work but I can not get it to work in my code.
Here are the returned data from `curl_getinfo($ch);'
array (size=26)
'url' => string 'https://dev-fronapp.com/api/login' (length=47)
'content_type' => string 'application/json; charset=utf-8' (length=31)
'http_code' => int 200
'header_size' => int 268
'request_size' => int 384
'filetime' => int -1
'ssl_verify_result' => int 20
'redirect_count' => int 0
'total_time' => float 2.14
'namelookup_time' => float 0
'connect_time' => float 0.344
'pretransfer_time' => float 1.047
'size_upload' => float 253
'size_download' => float 3991
'speed_download' => float 1864
'speed_upload' => float 118
'download_content_length' => float 3991
'upload_content_length' => float 253
'starttransfer_time' => float 2.125
'redirect_time' => float 0
'redirect_url' => string '' (length=0)
'primary_ip' => string '' (length=13)
'certinfo' =>
array (size=0)
empty
'primary_port' => int 443
'local_ip' => string '' (length=12)
'local_port' => int 10484
Your modify code and changes made are
CURL SSL verifier is set to false
JSON error handling
API runtime exceptions
return values if api status is 1/200/ok/true.
<?php
if (isset($_POST['uname'], $_POST['password'])){
$uname = test_input($_POST['uname']);
$pw = test_input($_POST['password']);
$data = array(
"loginEmail" => $uname,
"loginPassword" => $pw,
"clientURI" => "https://hk2.notify.windows.com/?token=AwYAAAC2bng45wHDpXWajWLTxGM1gxP1DcLlHTL8%2bhOfKVbkpYjl1U9tnUGrW4FSmwuiiWPKEyvSUoO5v9nfzFWZ097kdUeN8xDpAlp96Ilk3LCSN0sQNFuyU%3d"
);
$url_send ="https://dev-frontserver.com/api/login";
$str_data = json_encode($data);
$headers = array("Content-Type: application/json");
echo "result = " .sendPostData($url_send, $str_data, $headers);
}
function sendPostData($url, $post, $headers){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($post));
//avoid checking ssl verifier
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
$result = curl_exec($ch);
if($result === false) {
throw new \RuntimeException(
'API call failed with cURL error: ' . curl_error($ch)
);
}
curl_close($ch);
$apiResponse = json_decode($result, true);
// Make sure we got back a well-formed JSON string and that there were no errors when decoding it
$jsonError = json_last_error();
if($jsonError !== JSON_ERROR_NONE) {
throw new \RuntimeException(
'API response not well-formed (json error code: ' . $jsonError . ')'
);
}
// Print out the response details
if($apiResponse['status'] === 1)//check field where
{
// No errors encountered
return $result;
}
else {
// An error occurred
die("Error:");//show error message from api
}
}
?>
Please test code and let me know if any modifications needed
This question is similar to: Freebase API call works in browser but fails with curl but no answers there.
I have this URL that works in the browser:
https://www.googleapis.com/freebase/v1/search?query=Cee+Lo+Green&filter=%28all+type%3A%2Fmusic%2Fartist+created%3A%22The+Lady+Killer%22%29&limit=10&key=my_key_here
Returns:
{"status":"200 OK","result":[{"mid":"/m/01w5jwb","id":"/en/cee_lo_1974","name":"Cee Lo Green","notable":{"name":"Singer-songwriter","id":"/m/016z4k"},"lang":"en","score":814.733643}],"cost":6,"hits":2}
However, in PHP, the following code doesn't work:
<?php
namespace com\voting;
class Freebase{
public function query(){
$service_url = 'https://www.googleapis.com/freebase/v1/search';
$params = array(
'query' => 'Cee Lo Green',
'filter' => '(all type:/music/artist created:"The Lady Killer")',
'limit' => 10,
'key' => API_KEY );
$url = $service_url . '?' . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
return $response;
}
}
?>
A dump of curl, gives this:
array (size=22)
'url' => string 'https://www.googleapis.com/freebase/v1/search?query=Cee+Lo+Green&filter=%28all+type%3A%2Fmusic%2Fartist+created%3A%22The+Lady+Killer%22%29&limit=10&key=my_key_here' (length=191)
'content_type' => null
'http_code' => int 0
'header_size' => int 0
'request_size' => int 0
'filetime' => int -1
'ssl_verify_result' => int 0
'redirect_count' => int 0
'total_time' => float 0.047
'namelookup_time' => float 0
'connect_time' => float 0.063
'pretransfer_time' => float 0
'size_upload' => float 0
'size_download' => float 0
'speed_download' => float 0
'speed_upload' => float 0
'download_content_length' => float -1
'upload_content_length' => float -1
'starttransfer_time' => float 0
'redirect_time' => float 0
'certinfo' =>
array (size=0)
empty
'redirect_url' => string '' (length=0)
I thought maybe a connectivity issue, but I tried a regular http URL and curl showed that it was able to get the content of the page.
Update 1
I should have done my homework before posting. With Wireshark, I can see the following text:
Alert Message
Level: Fatal (2)
Description: Unknown CA (48)
Is my setup improperly configured?
Thanks, in just a script:
<?php
$service_url = 'https://www.googleapis.com/freebase/v1/search';
$params = array(
'query' => 'Cee Lo Green',
'filter' => '(all type:/music/artist created:"The Lady Killer")',
'limit' => 10,
'key' => API_KEY );
$url = $service_url . '?' . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//WARNING: this would prevent curl from detecting a 'man in the middle' attack
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
return $response;
?>
This answer solved it for me:
HTTPS and SSL3_GET_SERVER_CERTIFICATE:certificate verify failed, CA is OK
Final solution:
<?php
namespace com\voting;
class Freebase{
public function query(){
$service_url = 'https://www.googleapis.com/freebase/v1/search';
$params = array(
'query' => 'Cee Lo Green',
'filter' => '(all type:/music/artist created:"The Lady Killer")',
'limit' => 10,
'key' => API_KEY );
$url = $service_url . '?' . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//WARNING: this would prevent curl from detecting a 'man in the middle' attack
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
return $response;
}
}
?>