Use brackets with wp_remote_get - php

I need to get data from another website via WP-REST-API, but the response is too large. So I want to trim it down to only the data I need:
wp_remote_get('https://www.example.com/wp-json/wp/v2/custom_post_type/?_fields[]=link&_fields[]=content&_fields[]=featured_media&per_page=100');
When I use brackets ([]) inside the URL I get a fatal error.
Is there another way to limit the response for the wp_remote_get() function?

Related

I can't send a parameter containing curly brackets via get method in php curl

I'm working on application APIs. I'm trying to create a bot to pull data with php. In short, I need to send a parameter under the GET method, this parameter contains a json data, so I cover it with curly brackets, but when I do this, php curl does not work properly and I make a request to a url that I do not want, can you help me please?
Use json_encode() to create the JSON, and urlencode() to encode it properly for use in a URL.
$param = urlencode(json_encode(['need' => 'help']));
curl_setopt($ch, CUROPT_URL, "example.com/api/?params=$param");

How to get data in php curl with square brackets query?

I try to get data with libcurl in PHP and my query, value in query, contains query brackets
?conditions[prs_id]=7&conditions[date]=[2019-03-26 TO 2019-04-03]&page=1&limit=1
When I send this with Postman I get object, but with curl i get error 400 which says this is bad request.
but with
?conditions[prs_id]=7&conditions[date]=2019-03-26
I get answer and requested objects.
So The problem is with value with brackets [2019-03-26 TO 2019-04-03]
I tried with url encoding, changing brackets to url codes but it won't worked.
How to properly send this with libcurl in PHP? in Postman works
Try using urlencode() function in the query. I am not sure it will work or not but you can give it a try
Use this function to build query params in url http_build_query

Batch parameter must be a JSON array

Im trying to make following BATCH request on Facebook Graph API
I encode this using urlencode
/?batch=[{"method":"POST","relative_url":"23843240845450549","body":"end_time=2019-03-29T19:59:59-0300&lifetime_budget=5328"}]
This is the encoded request
/?batch=%5B%7B%22method%22%3A%22POST%22%2C%22relative_url%22%3A%226118436634612%22%2C%22body%22%3A%22end_time%3D2019-03-29T19%3A59%3A59-0300&lifetime_budget=328921%22%7D%5D
But i get
"Batch parameter must be a JSON array",
I know the error is around the '&', Im unable to find the way to put multiple fields on body request
Have also tried
/?batch=[{"method":"POST","relative_url":"23843240845450549","body":"end_time=2019-03-29T19:59:59-0300&lifetime_budget=5328"}]
and also
/?batch=[{"method":"POST","relative_url":"23843240845450549","body":"end_time=2019-03-29T19:59:59-0300&amplifetime_budget=5328"}]
Finally i found that i had to encode the & into %26
So this is working:
/?batch=%5B%7B%22method%22%3A%22POST%22%2C%22relative_url%22%3A%226118436634612%22%2C%22body%22%3A%22end_time%3D2019-03-29T19%3A59%3A59-0300%26lifetime_budget%3D328921%22%7D%5D

Single quotes in JSON from PHP causing problems

NOTE: this was a completely different question until I realized where the problem really was.
My current issue is that I am trying to output some JSON from PHP for use by jQuery. I am doing this cross-domain so I am using "JSONP". I have narrowed the problem down to the fact that there are single quotes in my JSON so when I output with the callback function I end up getting too many single quotes.
I have tried calling str_replace("'","\'",$value) in PHP and it seems to output as \\' in my JSON rather than \' which is apparently not readable by jQuery (though online JSON validators say the JSON is valid.
So what I need to know is how to get only a single slash in my string inside of PHP rather than 2 slashes.
What I'm gathering is this:
jQuery uses _ as a callback parameter to JSONP requests. It will automatically append ?_=jQuery<random_numbers> basically telling the server to call this unique function name when it returns. As such, as of recently (within the last year I want to say) .ajax takes care of making a callback function for you, then kind of "re-routes" it to a function you specify within the success property.
Also, your PHP code is using $_GET['callback'] when it now should be using $_GET['_'] (to stay more in-line with jQuery and what it's sending).
The "jQuery was not called" is just jQuery notifying you that anticipated callback wasn't included in the response, and it was looking to make sure it was getting called.
Short answer (as I see it) reference $_GET['_'] instead of $_GET['callback'] to satisfy jQuery.
try to use this in your PHP:
echo $_GET['callback'] . '(' . json_encode($your_array_data) . ')';
Regards.

Problem with php and json

Could some one please help me out on this I have the following json string
string(1223) "YAHOO.Finance.SymbolSuggest.ssCallback({"ResultSet":{"Query":"google","Result":[{"symbol":"GOOG","name": "Google Inc.","exch": "NMS","type": "S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"GOOG.MX","name": "GOOGLE-A","exch": "MEX","type": "S","exchDisp":"Mexico","typeDisp":"Equity"},{"symbol":"GGQ1.F","name": "GOOGLE-A","exch": "FRA","type": "S","exchDisp":"Frankfurt","typeDisp":"Equity"}]}})"
But I cannot seem to get anywhere with it. Basically I want to just loop out the the results which are
[{"symbol":"GOOG","name": "Google Inc.","exch": "NMS","type": "S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"GOOG.MX","name": "GOOGLE-A","exch": "MEX","type": "S","exchDisp":"Mexico","typeDisp":"Equity"},{"symbol":"GGQ1.F","name": "GOOGLE-A","exch": "FRA","type": "S","exchDisp":"Frankfurt","typeDisp":"Equity"}]
Sorry my question is how can I loop or even print the first result for example
{"symbol":"GOOG","name": "Google Inc.","exch": "NMS","type": "S","exchDisp":"NASDAQ","typeDisp":"Equity"}
Your string is not JSON, it is JSON-in-Script. Notice the fragment that says:
YAHOO.Finance.SymbolSuggest.ssCallback(...)
When a browser receives the above mentioned script (actually a javascript code) it will call the YAHOO.Finance.SymbolSuggest.ssCallback function, passing the JSON data as the argument.
You did not mention if you want to access the JASON data on the server side or client? It its server side (PHP) then you can use regular expressions or string replacement functions to extract the portion you like. The you can use json_decode() function to convert the resulting string into an associative array.
Edit ----
A quick and dirty hack for converting JSONP to JSON:
<?php
$text = 'YAHOO.Finance.SymbolSuggest.ssCallback({"ResultSet":{"Query":"google","Result":[{"symbol":"GOOG","name": "Google Inc.","exch": "NMS","type": "S","exchDisp":"NASDAQ","typeDisp":"Equity"},{"symbol":"GOOG.MX","name": "GOOGLE-A","exch": "MEX","type": "S","exchDisp":"Mexico","typeDisp":"Equity"},{"symbol":"GGQ1.F","name": "GOOGLE-A","exch": "FRA","type": "S","exchDisp":"Frankfurt","typeDisp":"Equity"}]}})';
# //CONVERT JSONP to JSON\\
$text = preg_replace('/.+?({.+}).+/', '$1', $text);
# \\CONVERT JSONP to JSON//
$data = json_decode($text);
var_dump($data);
var_dump($data->ResultSet->Result[0]);
var_dump($data->ResultSet->Result[0]->symbol);
var_dump($data->ResultSet->Result[0]->name);
# etc etc
?>
Your result is not just a JSON string, it's a JSON string prepended by a call to a JSON function. This is quite certainly a JSONP call.
You must write the YAHOO.Finance.SymbolSuggest.ssCallback(data) javascript function and get the Json there. Check the JSONP query, you should be able to alter the name of this backreference function if you want another name, it's usually on of the parameter in the GET query.
Now you are maybe calling it directly from PHP and you are not in js envirronment. so you must write something in your PHP code to remove the YAHOO.Finance.SymbolSuggest.ssCallback( part and the ) at the end before parsing it as JSON data..

Categories