PHP Array values from file_get_contents [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I declared one variables like this
echo $OPTIONS="500=>250.00, 1000=>500.00,2500=>1100.00,5000=>2250.00";
and
I got this variables through file_get_contents() functions.
$contents = file_get_contents(SERVICE_URL."options_config.php?options=".$OPTIONS);
$package=array($contents)
foreach($package as $pack=>$price)
{
echo $pack;
}
But I got 0 values. What is the problem?
print_r($package);
The result is :
Array ( [0] => 500=>250.00, 1000=>500.00,2500=>1100.00,5000=>2250.00 )
I want the result like this
500 as 250.00
1000 as 500.00

I think what you are looking for is serialize and unserialize
Example: test.php
<?php
// Handle Get Request
// This portion of your code can be on another file
//
if (isset($_GET['getOptions']))
{
$myOptions = array(
500 => 250.00,
1000 => 500.00,
2500 => 1100.00,
5000 => 2250.00
);
exit(serialize($myOptions));
}
// Sample Usage
$options = file_get_contents('http://localhost/test.php?getOptions');
$options = unserialize($options);
// Debug
var_dump($options);
?>
Outputs:

Related

Use JSON in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I use some website that give me information about IP but the information that that website return in JSON and I don't know the JSON. I want to use this to check the user if it is from IR do something but I dont know how to use JSON in php,
Here is the JSON that the website return:
{"address":"0.0.0.0.0","country":"IR","stateprov":"somewhere ","city":"Tehr\somewhere (somewhere)"}
I want to save the country in a variable and add this code to my website:
<?php
if($country == 'IR'){
//Do somethong
}
$country is the country name that return from the website,
You'll need to use json_decode().
$s = '{"address":"0.0.0.0.0","country":"IR","stateprov":"somewhere ","city":"Tehrsomewhere (somewhere)"}';
$d = json_decode($s);
Which returns:
stdClass Object
(
[address] => 0.0.0.0.0
[country] => IR
[stateprov] => somewhere
[city] => Tehrsomewhere (somewhere)
)
That would allow you to check the country/other fields like this:
if($d->country == 'IR') {
// do something
}
NOTE: you had an error (invalid json) in your "city" field, the \ makes it invalid.
Example
You can ensure that your json is valid by checking it at JSON Lint.
I think you are looking for the function json_decode.It decodes the JSON string
See the documentation here
You have to first decode this json string.
$data = '{"address":"0.0.0.0.0","country":"IR","stateprov":"somewhere ","city":"Tehr\somewhere (somewhere)"}';
$decodeData = json_decode($data);
Then use this decode json string in php like this.
if($decodeData->country == 'IR'){
//Do somethong
}
First of all i would like to inform you that given json is not valid. "city" : "Tehr\somewhere (somewhere)" is not valid because of "\".
So change it into below given format.
$jsonEncode = { "address": "0.0.0.0.0","country": "IR","stateprov": "somewhere ","city": "There somewhere (somewhere)"}
$jsonDecode = json_decode($jsonEncode,true);
Now you will get the value in array format.
Array(
[address] => 0.0.0.0.0
[country] => IR
[stateprov] => somewhere
[city] => There somewhere (somewhere)
);
print_r($jsonDecode['city']); will give you city name or details

PHP Parse error: syntax error, unexpected ':' line 7 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Please i dont know how to remove this error on line 7 onto this $rest_url = “https://mydomain.com/rest-api/analytics/HTML/”.$login.”/”.$hash.”/”.$timestamp.”.do”;
$timestamp = round(microtime(true) * 1000);
$login = ‘username’;
$password = ‘password’;
$hash = md5(md5($password).$timestamp);
$rest_url = “https://mydomain.com/rest-api/analytics/HTML/”.$login.”/”.$hash.”/”.$timestamp.”.do”;
$post_data = array(
‘range’ => ‘LAST_7_DAYS’,
‘groupBy’ => ‘PLACEMENT’
);
/* Options of HTTP request; http key should be used when posting to https url */
$options = array(
‘http’ => array(
‘header’ => “Content-type: application/x-www-form-urlencoded\r\n”,
‘method’ => ‘POST’,
‘content’ => http_build_query($post_data)
)
);
/* Actual call to REST URL */
$context = stream_context_create($options);
$result = file_get_contents($rest_url, false, $context);
echo($result);
Your string starting and ending characters are incorrect.
You are using ‘ and “ ... replace them with ' and " to make your code valid.
since file_get_contents() is not reliable in remote connections,
You have to use CURL to overcome this http error.
For more info on curl_init read this

How can I parse the following Array with php? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
string: [Name] => google [AF] => 0 [AX] => 2 [AL] => 1 [DZ] => 0 .....
output:
[AX,2]
[AL,1]
the countries with [0] entries are being excluded in the list
I have tried using sql but i want for php
Select '[AF,' + LTrim(Cast(Sum(AF) as varChar(5))) + ']'
From Table Where name = 'Andy'
Union
Select '[AS,' + LTrim(Cast(Sum(AS) as varChar(5))) + ']'
From Table Where name = 'Andy'
Union
Select '[AQ,' + LTrim(Cast(Sum(AQ) as varChar(5))) + ']'
From Table Where name = 'Andy'
and also
http://sqlfiddle.com/#!2/42756/5
but i need in php
A simple foreach loop will do the trick:
foreach($array AS $key=>$value){
if($value!=0){
echo "$key,$value";
}
}

Cause of "Method name must be a string" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
public function run() {
$this->step = $this->$_POST("step", 1);
$this->xml = new XMLFile();
$common_data = array(
'STEPCONTENT' => $this->get_step_content(),
'STEPNUMBER' => $this->step,
'STEPTITLE' => $this->get_step_title()
);
echo $this->parse($this->common_template, $common_data);
This gives the exception:
Fatal error: Method name must be a string in
C:\xampp\htdocs\test\openad\install\InstallOpenAdServer.php on line 674
Why?
This is the culprit
$this->step = $this->$_POST("step", 1);
You cannot use an $_POST super global array as a function. If you are trying to access the vairable from the $_POST, you can simple do this
$this->step = $_POST["step"];

PHP: Catchable fatal error: Object of class stdClass could not be converted to string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I get the following dump & error when running the attached code. What I'm confused by is that $procID appears to be returned as a string, but as soon as I attempt to pass it again, its an object? How do I get it to be/stay a string? Thanks.
object(stdClass)#2 (1) {
["processId"]=> string(13)
"Genesis114001" } string(311)
"Genesis114001" string(293) " Genesis
" Catchable fatal error: Object of
class stdClass could not be converted
to string in
C:\wamp\www\SugarCE\testSOAPShawn.php
on line 15
<?php
set_time_limit(0);
require_once('nusoap.php');
require_once('BenefitSOAP.php'); //WSDL to PHP Classes
$client = new SoapClient('C:\wsdl\BenefitDeterminationProcess_BenefitDialogueServiceSOAP.wsdl', array('trace' => 1));
$procID = $client->start(array("prefix"=>"Genesis"));
$respXML = $client->__getLastResponse();
$requXML = $client->__getLastRequest();
echo "<p/>";
var_dump($procID);
//echo "<p/>";
var_dump($respXML);
//echo "<p/>";
var_dump($requXML);
$exchange = $client->exchangeOptions(array("processId"=>$procID)); //LINE 15
$end = $client->stop(array("processId"=>$procID));
?>
Whatever the $client->start() method is returning, it is typed as an object. You can access the properties of the object using the -> operator:
$procID = $client->start(array("prefix"=>"Genesis"));
...
$exchange = $client->exchangeOptions(array("processId"=>$procID->processId));
This was probably an array, but is getting typed into an object. Thus, you end up with the stdClass.
Another (and possibly better) way to do this is to type the return. That way, you don't have to make a new array for later passing as argument:
$procID = (array) $client->start(array("prefix"=>"Genesis"));
...
$exchange = $client->exchangeOptions($procID);
$end = $client->stop($procID);

Categories