How to use an API in my program?

Sorry, I was not available much lately. I just reviewed your posted code. I do not think you can index cURL objects as an array entry. An array of objects may interfere with the use of the object. I understand why you might want to index an array of material codes sent by Json, but there is no reason to create an array of all the cURL objects. Why waste the processing time to index them all. Just loop thru the material codes, process the cURL for each and store the results into your indexed results array. Just do not using indexing of the cURL objects. ( Meaning $client. as each curl call is a process of it’s own. )

Not sure if that makes sense or not, but, try removing all the indexing from the curl sections…
( By the way, what line gave you the “cannot use temporary expression” error??? )

1 Like

Dear Ernie, thanks again for getting back to me,
I think it is better to review the problem from the beginning:
I want to use an API which generates Importation_quantity based on the material_code.
In my program, the clients should enter material_code in a grid or it can be populated from our database. but, in a grid, there are some columns, and one of them is named toward_order. API should return its value based on its material_name.
So, I wrote the below code:

pmServer = “http://localhost:1367/rest-api2/api-alis.php?material_code=”;
$lenGrid = count(@=planning_evaluation);
$material_code =array();
for($x = 1; $x <= $lenGrid;$x++ ) {
$material_code[$x] = @=planning_evaluation[$x][‘material_code’];

$url[$x] = $pmServer.$material_code[$x];
$client[$x] = curl_init($url[$x]);
curl_setopt($client[$x],CURLOPT_RETURNTRANSFER,true);
$response[$x] = curl_exec($client[$x]);
$result[$x] = json_decode($response[$x]);
@=planning_evaluation[$x][‘toward_order’] = $result[$x];
foreach ($result[$x] as $results[$x]) {
echo "{$results[$x]->material_code} : {$results[$x]->importation_quantity}
";
}
$quantity2[$x] = $results[$x]->importation_quantity;
}

I can get correct result to ($response[$x] = curl_exec($client[$x]);
But, when I add ($result[$x] = json_decode($response[$x])). I see this error: Cannot use temporary expression

And one more thing: would you please say your idea with code? I mean, if you think those indexes are redundant, please correct it to understand your mention.
Again thank you very much and very best regard.

Just a few minor changes. But, all of the indexing that there is no need to process the cURL process in arrays. You only want the results, not the in-between results. Not an issue really if it is working, just a waste of server processing time. If you have a lot of users, it would waste a lot of process time.

The json_decode creates a temporary result which then alters the $result array. I think to fix that you would just need to get the decoded json data into a variable then save it as needed. Loosely like this:
$response[$x] = curl_exec[$ch]; // Using the above system…
$temp_variable = json_decode($response[$x]; // Name this temp variable anything you want
$result[$x] = $temp_variable;
This way, you are not using a temporary expression since you are saving it into an actual variable first. Seems like this would not be a problem, but the json process is complicated behind the scenes and I suspect this is hard to move into an array.

Good luck… Let us know how it works out…

1 Like

Dear Ernie, thanks for your valuable time,
I tested your new code(I added $temp_variable = json_decode($response[$x])
I yet see the previous error.
Cannot use temporary expression
So, what should I do to solve it? Do you have another ideia?
Thanks in advance!

Which line is the error on?

1 Like

Dear Ernie, thanks again
It shows something like that:
[toward_order] => Array ( [0] => stdClass Object ( [material_code] => 1 [importation_quantity] => 3025 ) )
The column get object object instead of correct value.
It is obvious that in behind the scenes, there are correct values, but stdClass object! I do know know really!
I guess [$temp_variable = json_decode($response[$x]);] can not maintain array!
Of course, I’m not sure.
but the error is related to json_decode!
how to keep array in json_decode? or I was wondering if you could present another code solution.
Very best regards

Well, as we discussed a long time ago, the json coding does not create arrays. It creates a json object. But, you can create an array by adding the ", 1 " option. To test this, you cold try this. The DIE command would just force it to end at that point and display the value it pulls out. That way, you can see what data you are really getting from the table.

1 Like

Thanks again,
It returns something like this:
Array
(
[1] =>
)

json_decode($response[$x]) returns Object object and cannot return correct values, however before it when I call its previous line ($response[$x] = curl_exec($client[$x]);), it returns correct values. I do not know why does jason_decode return Object object.
Looking forward to your valuable comments.

Sponsor our Newsletter | Privacy Policy | Terms of Service