Getting Response from DSTK (Data Science Toolkit) with Guzzle / Goutte - php

I'm trying to get JSON data back from the Data Science Toolkit, but the response I am getting doesn't have it. I am trying to pass a string object (json encoded from an array of addresses) to the dstk field, to return a list of coordinates.
Here's the following code snippet:
$client = new \GuzzleHttp\Client(['base_uri' => 'http://www.datasciencetoolkit.org/']);
$request = $client->post('street2coordinates', ['form_params' => ['body' => json_encode($addresses)] ]);
And when I die dump
dd($request->getBody()),it just returns the ff object. or even just $request, I can't pull json data on my search I am expecting Latitude/Longitude coordinates - but I have no idea how/where to get them.
object(GuzzleHttp\Psr7\Stream)[215]
private 'stream' => resource(8, stream)
private 'size' => null
private 'seekable' => boolean true
private 'readable' => boolean true
private 'writable' => boolean true
private 'uri' => string 'php://temp' (length=10)
private 'customMetadata' =>
array (size=0)
empty
I have tried using the Goutte client as well, to no avail. Even trying different methods like ->json() or ->toJson(), no dice. Help is appreciated. Thanks!

You have to force the body to a string, for example by echo'ing it, or casting it explicitly.
echo $response->getBody();
Or
dd((string)$response->getBody());

Related

JSON looping to get playtime_forever for specific games using Steam API [duplicate]

I am trying to write a script that gets a JSON file from a remote location (in this case being twitch.tv) using cURL (don't think that part is too relevant, though I better mention it anyway). For example purposes, lets say the JSON object it returns looks something like this after being stored in a variable:
$json_object = {"_links":{"self":"https://api.twitch.tv/kraken/streams/gmansoliver","channel":"https://api.twitch.tv/kraken/channels/gmansoliver"},"stream":null}
I access the "stream" property, I have tried the follow code:
<?php
$json_object = {"_links":{"self":"https://api.twitch.tv/kraken/streams/gmansoliver","channel":"https://api.twitch.tv/kraken/channels/gmansoliver"},"stream":null}
$json_decoded = json_decode($json_object, true);
echo $json_decoded->stream;
?>
When I try this, I get the error "Notice: Trying to get property of non-object in D:\Servers\IIS\Sites\mysite\getstream.php on line 48".
Am I using json_decode() wrong, or is there something wrong with the JSON object I am being sent from twitch?
Edit:
I now have the JSON object:
{"access_token": "qwerty1235","refresh_token": "asdfghjkl=","scope": ["user_read"]}
If I try to decode it using json_decode() I get the following error: Object of class stdClass could not be converted to string. Any advice?
Thanks in advance for any help
You're decoding the JSON into an array. json_decode($json_object, true);
Will return an array
array (size=2)
'_links' =>
array (size=2)
'self' => string 'https://api.twitch.tv/kraken/streams/gmansoliver' (length=48)
'channel' => string 'https://api.twitch.tv/kraken/channels/gmansoliver' (length=49)
'stream' => null
If you remove the second parameter and run it as json_decode($json_object)
object(stdClass)[1]
public '_links' =>
object(stdClass)[2]
public 'self' => string 'https://api.twitch.tv/kraken/streams/gmansoliver' (length=48)
public 'channel' => string 'https://api.twitch.tv/kraken/channels/gmansoliver' (length=49)
public 'stream' => null
See the documentation, When TRUE, returned objects will be converted into associative arrays.
You have set the second parameter ($assoc) of json_decode() to true, which means it's going to return an associative array instead of an object. You then tried to reference the object style. If you are setting the second parameter to true, you need to use the associative array style to access the stream content. It would be:
$json_decoded['stream']
If you set the $assoc parameter to false (or do not specify the parameter) then you can reference it as an object:
$json_decoded->stream
If you do var_dump on the $json_decoded variable you will see what it looks like. This is a good way to see what you are working with.

Trying to get property of non-object [json] centos [duplicate]

I am trying to write a script that gets a JSON file from a remote location (in this case being twitch.tv) using cURL (don't think that part is too relevant, though I better mention it anyway). For example purposes, lets say the JSON object it returns looks something like this after being stored in a variable:
$json_object = {"_links":{"self":"https://api.twitch.tv/kraken/streams/gmansoliver","channel":"https://api.twitch.tv/kraken/channels/gmansoliver"},"stream":null}
I access the "stream" property, I have tried the follow code:
<?php
$json_object = {"_links":{"self":"https://api.twitch.tv/kraken/streams/gmansoliver","channel":"https://api.twitch.tv/kraken/channels/gmansoliver"},"stream":null}
$json_decoded = json_decode($json_object, true);
echo $json_decoded->stream;
?>
When I try this, I get the error "Notice: Trying to get property of non-object in D:\Servers\IIS\Sites\mysite\getstream.php on line 48".
Am I using json_decode() wrong, or is there something wrong with the JSON object I am being sent from twitch?
Edit:
I now have the JSON object:
{"access_token": "qwerty1235","refresh_token": "asdfghjkl=","scope": ["user_read"]}
If I try to decode it using json_decode() I get the following error: Object of class stdClass could not be converted to string. Any advice?
Thanks in advance for any help
You're decoding the JSON into an array. json_decode($json_object, true);
Will return an array
array (size=2)
'_links' =>
array (size=2)
'self' => string 'https://api.twitch.tv/kraken/streams/gmansoliver' (length=48)
'channel' => string 'https://api.twitch.tv/kraken/channels/gmansoliver' (length=49)
'stream' => null
If you remove the second parameter and run it as json_decode($json_object)
object(stdClass)[1]
public '_links' =>
object(stdClass)[2]
public 'self' => string 'https://api.twitch.tv/kraken/streams/gmansoliver' (length=48)
public 'channel' => string 'https://api.twitch.tv/kraken/channels/gmansoliver' (length=49)
public 'stream' => null
See the documentation, When TRUE, returned objects will be converted into associative arrays.
You have set the second parameter ($assoc) of json_decode() to true, which means it's going to return an associative array instead of an object. You then tried to reference the object style. If you are setting the second parameter to true, you need to use the associative array style to access the stream content. It would be:
$json_decoded['stream']
If you set the $assoc parameter to false (or do not specify the parameter) then you can reference it as an object:
$json_decoded->stream
If you do var_dump on the $json_decoded variable you will see what it looks like. This is a good way to see what you are working with.

how to set the request method as post and add variables to post array in zend request object

I have an intermediate form processor that needs to take the POST data from the form and manipulate it then put it back into the post superglobal using Zend_Controller_Request_Http. Also, I need to set the new request method as type POST using Zend_Controller_Request_Http
Using $request->setParam() does that add it to the post data or just a hash of parameters?
So, in summary:
- Set the Zend_Controller_Request_Http request object method as type POST
- Set the modified POST data to the new POST request data (I imagine its setting it into the superglobals but i want to use Zend Request Object instead).
Thanks.
Well, not sure what you are trying but let me just give you a few pointers.
The Zend Request object is just exactly that, an object with everything about the Request. To be a bit more precise the original request and in that regard the elements in the request should not be altered. In many ways it just takes values from common sources and makes them available through this common interface. For instance, method getParams() returns values it finds in the $_GET or $_POST, i.e. you'll never have to worry if your data has been submitted via a GET or POST method.
This being said, you cannot change the superglobals through the Zend Request object because you should never do that. However, you can change them directly and then the Request object will reflect any changes since they are not permanently stored.
Here's an example of what you could (but should not) do in a controller action:
$params = $this->getRequest()->getParams();
$isPost = $this->getRequest()->isPost();
var_dump(compact('isPost','params'));
$_GET['something'] = 'new';
$_SERVER['REQUEST_METHOD'] = 'POST';
$params = $this->getRequest()->getParams();
$isPost = $this->getRequest()->isPost();
var_dump(compact('isPost','params'));
// here is result 1
array (size=2)
'isPost' => boolean false
'params' =>
array (size=3)
'controller' => string 'index' (length=5)
'action' => string 'index' (length=5)
'module' => string 'default' (length=7)
// here is result 2
array (size=2)
'isPost' => boolean true
'params' =>
array (size=4)
'controller' => string 'index' (length=5)
'action' => string 'index' (length=5)
'module' => string 'default' (length=7)
'something' => string 'new' (length=3)

Yii2 Object of class Closure could not be converted to string

I've got error
Object of class Closure could not be converted to string
on this code
'class' => \dosamigos\grid\EditableColumn::className(),
'attribute' => 'remidi3',
'url' => function($data){return ['update?id=remidi3&dataid'.$data->id];},
'type' => 'text',
'editableOptions' => [
'mode' => 'inline',
]
even I've try to change
'url' => function($data){return ['update?id=remidi3&dataid'.$data->id];}
into
'url' => function($data){return 'update?id=remidi3&dataid'.$data->id;},
I need to display id in the URL of editable grid, somebody can help me?
According to source code and PHPDoc, you can't specify closure here.
PHPDoc says:
/**
* #var string the url to post
*/
public $url;
Usage in source code:
if ($this->url === null) {
throw new InvalidConfigException("'Url' property must be specified.");
}
...
$url = (array) $this->url;
$this->options['data-url'] = Url::to($url);
As you can see, it's converted to array and then processed by Url::to(), so the valid types are string and array.
I don't think you need to specify id in url, it should be taken automatically depending on row you working with.

json_decode() returning error "Notice: Trying to get property of non-object"

I am trying to write a script that gets a JSON file from a remote location (in this case being twitch.tv) using cURL (don't think that part is too relevant, though I better mention it anyway). For example purposes, lets say the JSON object it returns looks something like this after being stored in a variable:
$json_object = {"_links":{"self":"https://api.twitch.tv/kraken/streams/gmansoliver","channel":"https://api.twitch.tv/kraken/channels/gmansoliver"},"stream":null}
I access the "stream" property, I have tried the follow code:
<?php
$json_object = {"_links":{"self":"https://api.twitch.tv/kraken/streams/gmansoliver","channel":"https://api.twitch.tv/kraken/channels/gmansoliver"},"stream":null}
$json_decoded = json_decode($json_object, true);
echo $json_decoded->stream;
?>
When I try this, I get the error "Notice: Trying to get property of non-object in D:\Servers\IIS\Sites\mysite\getstream.php on line 48".
Am I using json_decode() wrong, or is there something wrong with the JSON object I am being sent from twitch?
Edit:
I now have the JSON object:
{"access_token": "qwerty1235","refresh_token": "asdfghjkl=","scope": ["user_read"]}
If I try to decode it using json_decode() I get the following error: Object of class stdClass could not be converted to string. Any advice?
Thanks in advance for any help
You're decoding the JSON into an array. json_decode($json_object, true);
Will return an array
array (size=2)
'_links' =>
array (size=2)
'self' => string 'https://api.twitch.tv/kraken/streams/gmansoliver' (length=48)
'channel' => string 'https://api.twitch.tv/kraken/channels/gmansoliver' (length=49)
'stream' => null
If you remove the second parameter and run it as json_decode($json_object)
object(stdClass)[1]
public '_links' =>
object(stdClass)[2]
public 'self' => string 'https://api.twitch.tv/kraken/streams/gmansoliver' (length=48)
public 'channel' => string 'https://api.twitch.tv/kraken/channels/gmansoliver' (length=49)
public 'stream' => null
See the documentation, When TRUE, returned objects will be converted into associative arrays.
You have set the second parameter ($assoc) of json_decode() to true, which means it's going to return an associative array instead of an object. You then tried to reference the object style. If you are setting the second parameter to true, you need to use the associative array style to access the stream content. It would be:
$json_decoded['stream']
If you set the $assoc parameter to false (or do not specify the parameter) then you can reference it as an object:
$json_decoded->stream
If you do var_dump on the $json_decoded variable you will see what it looks like. This is a good way to see what you are working with.

Categories