Trying to get property of non-object with syntax error - php

I created a instagram feed for opencart and when i wrote the code in normal php file it worked perfectly but when i transformed to opencart controller view it returns when
json_last_error();
int(4) syntax error
and here is my code in controller
$response= utf8_encode(file_get_contents("https://www.instagram.com/user/?__a=1"));
$response= str_replace(array('ï',''), '',$response);
$response= json_decode($response);
$data['orgimages'] = json_last_error();
i tried so many solutions or filtering out methods and that is my final code as it was changed so many times from stackoverflow answer solution for other questions but nothing seems to be worked out and even their forums didn't worked out with my issue.

Your error indicates that your json is wrongly formatted so try to echo it and validate it. I am suspecting that your json is not a valid json.
Use this : https://jsonlint.com/ validator for json.
After this line :
$response= str_replace(array('ï',''), '',$response);
echo your $response variable and see how your json looks like

Related

Get SOAP response to xml

Stack Overflow is full with questions like this, I'm aware of that. But none of the questions and accepted answers have solved it for me.
I'm getting a SOAP response like this:
string(402482) "ZSDSD1BKA00_DE_02DE00010000001083212550JVBERi0xLjUNJeLjz9MNCjExNCAwIG9iag08PC9MaW5lYXJpemVkIDEvTCAxMDgzMjEvTyAxMTYv RSAxODY2Mi9OIDEyL1QgMTA3Nzk3L0ggWyA1MDMgMjc2XT4+DWVuZG9iag0gICAgICAgICAgICAg DQoxMzQgMCBvYmoNPDwvRGVjb2RlUGFybXM8PC9Db2x1bW5zIDQvUHJlZGljdG9yIDEyPj4vRmls dGVyL0ZsYXRlRGVjb2RlL0lEWzwxREZGRkVGMUM4Q0E0NTI4QjBBMUYxOUEyNTMwREQ2OD48RURD QTlGRDBFNTE4N0M0N0JGRUZCOEM0MjlCRTUwMjE+XS9JbmRleFsxMTQgMzZdL0luZm8gMTEzIDAg Ui9MZW5ndGggOTMvUHJldiAxMDc3OTgvUm9vdCAxMTUgMCBSL1NpemUgMTUwL1R5cGUvWFJlZi9X WzEgMiAxXT4+c3RyZWFtDQpo3mJiZBBgYGJg/g0kGEKABONRIMHiCiSYNEASK0DEG5AEP4i4CZKI ABHiQEJIDyTWDiL+AwmuVhCLDaTDCsQtBHHZgcReIMHIwAiyg4G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EkZMogHW+IJxHOOHBIg8Fn3CJW31JhCfB 7BufaAmI53m4C7xKML9/T6dVXtXxJl1qVLbKQGDkK8ihgQHyeEw/vjSf4iZtcNWnmBtgdmZWlQ0Y k4QzAw/3Jokw0KzsG1BWVgaOlUMDxHp5qKtlrJtH+vBhRhP90tB5ka71dEG/f/nxn17iEfMCzhXS 7J4XSkIYhOo08CyiTgsAIXeaQryi0wRGyp47Hj/S+XR6m271CvYbNAswlSv9ghYbCyJ89GfutgAg Xx7Iv2m+1SDcEU7jSavGd4TdMEGTXxu9ixitNnZ+PIa9k+1Slw0JI06n6eZvna2fGsIZY/SDtlMj wRWd5el6SzzfhPf2tnp5HEl4/iOPQWLAatwSLMzsLC2y/NdfSVboLbnXP8nXqkjLd2buPi00NTMw YewP8V1ipuKm1s3yid5XdZHmxvTNovEBzLxJ82w5Kde5JozGjS7+IWF7M1yKYOts01Q1/d67hLki RhPXHDj4Y7msVlm5pt+yclJus06fZfW2mT6lNfHE3gEm+fHZfU7bJVwIGj//aBBMUj9rg6qDBr5X
... but much longer.
Then I use simplexml_load_string to convert it, resulting in an apparently empty object. Now I've learned that this object is not empty though. print_r and var_dump are of no help and I used simplexml_tree.php for help. But I'm now stuck on how I can access a property within the result. I tried a bunch of different variations, most of them left me either a blank page, or an error 500.
This is the response I expect:
Trying to access property ORBLK, like I wrote, with many variations, has not been successful. Thats my current try (resulting a blank page):
$resultRow = (string)$xml->children('soap-env', true)->Body->children('n0', true)->Z_SDB_GET_SDB_FOR_MATERIALResponse->T_SDB->item->DOKAR;
How would I be able to access the property ORBLK?

file_get_contents() "failed to open stream: Network is unreachable" error on valid link

There's an extremely simple example of using Youtube API in php that somehow fails to work in my case, and I couldn't find a definitive solution for.
I desire to use the Youtube API without any wrapper, by myself, to get data of videos. The following search:list query works perfectly when I try accessing it from within by browser (as a link), but in php, I get that error when I try the same.
$apikey_YT = <my API key>;
$ytrequrl = "https://www.googleapis.com/youtube/v3/search?".
"part=snippet".
"&maxResults=50".
"&q=doom".
"&relatedToVideoId=h8j2zj-A5tE".
"&type=video".
"&key=${apikey_YT}";
$result = file_get_contents($ytrequrl);
var_dump($result);
The potential issues were URL encoding, or allowing allow-url-fopen, but neither seemed to help in my case: the former actually gave a new error message: No such file or directory.
What can I do?
Try it like this. Might be your formatting.
This works for me.
$ytrequrl = 'https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&q=doom&relatedToVideoId=h8j2zj-A5tE&type=video&key='.$apikey_YT;
$info= file_get_contents($ytrequrl);
$info= json_decode($info, true);
print("<pre>".print_r($info,true)."</pre>");

Rendering JSON with Cakephp

I'm trying to return a Json object from an outside form with Cakephp 2.1. I have all my data, and I've looked at the responses, and the problem is I get a massive error text before my JSON!
Heres the code I use to print the json:
if (!$validationPassed) {
if ($ExternalRequest) {
if ($shouldEchoErrors){
Configure::write('debug',0); // Disabled so theoretically it can't even print errors
$this->RequestHandler->setContent('json', 'application/json' );
$json = json_encode($this->Customer->validationErrors);
echo $json;
$this->autoRender = false ;
}
The JSON it prints is correct, {"Email":["EmailUnique"]}. However I get a huge preformatted error message before that, stack trace and everything.
It was a dumb moment, but the answer in case anyone else finds this:
Theres an error far earlier in the code, with debug on, cake will print it immediately. This will get wrapped into the json output.
The specific error turns out to not be relevant for this question, its just in the view.

json_decode - issues with api

I am trying to find the game someone is playing on twitch by using the api. I have setup the json_decode and it shows all of the content from the api. However whenever I try to print_r the game I get an error.
The error:
Notice: Undefined property: stdClass::$game in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\Portfolio -- Website\twitchstreaminfo\streaminfo.php on line31
PHP code:
$streamer = $_POST['username'];
$apiurl = "https://api.twitch.tv/kraken/streams/" . $streamer;
$apicontent = file_get_contents($apiurl);
$streamerinfo = json_decode($apicontent);
print_r($streamerinfo->game);
Try just doing the following first and verify the result:
print_r( $streamerinfo );
From what I can see with the API, the following should work:
print_r( $streamerinfo->stream->game );
Your error is saying that the propery of "$game" does not exist on the object "$streamerinfo". As suggested above, try priting the "$streamerinfo" to varify that it is valid. Another thing you can do to prevent this is to add the following :
if (isset($streamerinfo->game) {
print_r($streamerinfo->game);
}
That code will prevent this error, but not fix the problem. I suggest this as a final solution to help you solve the problem
if (isset($streamerinfo->game) {
print_r($streamerinfo->game);
} else {
print_r($streamerinfo);
}
This will keep your code from breaking in the way that it is now. But, it will also print "$streamerinfo" if it fails. This way you can see why it failed.

How to catch error in JSON response?

I'm getting the json data from the twitter using the wordpress function wp_remote_get as below:
$response = wp_remote_get('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=twitter');
$json = json_decode($response['body']);
and it works fine. However, it totally creates the problem on my page if the twitter returns some error. In that case the content on my page does not load after that error part. So how can I catch the error and continue, only if there is no error.
For example if the twitter limit exceeds, it returns following response:
{"request":"\/1\/statuses\/user_timeline.json?screen_name=twitter","error":"Rate limit exceeded. Clients may not make more than 150 requests per hour."}
From the above responsive, how can i get the error.
I'm trying following, but it does not work (my page does not load at all)
if (!isset($json['error']){
//continue
}
It doesn't work because you are trying to access $json as an array, which it is not. It's an object, so use:
if (!isset($json->error){
//continue
}
Your page doesn't load because it's causing a fatal error, which in turn produces a 500 internal server error. If you check your error log you'll find:
Fatal error: Cannot use object of type stdClass as array in ....
You need to use json_decode to "transform" your "json string" in an object.
$j = json_decode($json);
echo $j->{'error'}
Here you will find everything what you need to know about json_decode: http://php.net/manual/en/function.json-decode.php

Categories