Here is what I have so far. I see 9 results when I visit the URL and after using curl it still displays the 9 results after being printed out. When I used the json_decode function it only created 3 results. I have gone everywhere and havent found anything. A little help in the right direction would be good at this time.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
// This is what solved the issue (Accepting gzip encoding)
curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
$result = curl_exec($ch);
curl_close($ch);
So when I start to decode it
// decode the json
$resp = json_decode($result, true);
I only get 3 associative arrays because I do a
count($resp);
And figure it out that way. Is there a limit on how much the function json_decode() can do?
I was counting the array in the first brackets so it was always showing 3 results
See when you do a
count($resp);
You are only counting html_attributions, results and status.
If you want count the arrays you would do
count($resp['results']);
That would count the arrays in the results.
Related
I am trying to update my API with an update curl function but am struggling to work out why it isn't working
The areas where it may be wrong is key($id) I want it to
extract the ID column based on the key value for the ID array.
$URL I want to create the URL based on the const variables plus the resource name plus the value of the ID array that has been passed through rawurlencode.
So far this is my update code, but am wondering what area is wrong.
I can provide more info if needed and appreciate any help, thanks
<?php
function update(array $id,array $vaules, $resourcename)
$jsonData = json_encode($vaules);
key($id);
$url = DOMAIN.FOLDER.APIPATH.$resourcename.rawurlencode("/".$id);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,array ('content-type: application/json'));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,PUT);
curl_setopt($ch,CURLOPT_POSTFIELDS,$jsonData);
curl_exec($ch);
curl_getinfo(CURLINFO_HTTP_CODE);
}
The function key() returns the current key in an array (according to the internet pointer). Right now you're not doing anything with it, you're calling the function and not assigning it anywhere.
Did you mean to write: rawurlencode("/".key($id).$vaules);?
As your code is right now, assuming $id is an array, you're trying to convert an array into a string, which I doubt is what you want.
In some cases this works fine, in others like below, its not.
$xml_url = 'http://campusdining.compass-usa.com/Hofstra/Pages/SignageXML.aspx?location=Student%20Center%20Cafe';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xml_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux i686; ru; rv:1.9.3a5pre) Gecko/20100526 Firefox/3.7a5pre");
$data = curl_exec($ch);
$ce = curl_error($ch);
curl_close($ch);
// this is how I was doing it prior to today and it worked before
// preg_match_all("/<MealPeriod name=\"(.+?)\">([\w\W\r\n]*?)<\/MealPeriod>/i", $data, $output_array);
// this way doesnt show all the meal periods,
// but I need to know whats in between the MealPeriod tags
// preg_match_all('/<MealPeriod name="(.*?)">(.*?)<\/MealPeriod>/i', $data, $output_array);
// shows all the meal period names,
// but I need the above to work to store whats in between the MealPeriod tags in the $output_array[2]
preg_match_all('/<MealPeriod name="(.*?)">/i', $data, $output_array);
echo '<pre> '.print_r($output_array[1],1).'</pre>';
I tried this on a few regex live sites and 1 of them returned what I needed, while the second did not..
http://www.phpliveregex.com/ -- did work
https://regex101.com/ -- did not work
expected output would by the following for $output_array[1]:
Array
(
[0] => Breakfast
[1] => Every Day
[2] => Outtakes
[3] => Salad Bar
)
But it should also hold whats inbetween the MealPeriod tags in $output_array[2]
Any help would be greatly appreciated
This code below works, all I did was change the regex and change the printing.
The output on screen looks rather odd, because the second (.*?) to capture everything between <MealPeriod> and </MealPeriod> is capturing all the xml tags as well. If you look at the source code, you can clearly see this.
I would encourage you to work with an XML Parser to work with the document. I certainly have used regex to extract portions of XML documents before using a parser to convert them to objects but a parser is much better equipped to work with XML than regex (by leaps and bounds).
Everything is captured, but it is not being printed to the screen with <pre> tags. However, if you look at the source, everything is there.
<?php
$xml_url = 'http://campusdining.compass-usa.com/Hofstra/Pages/SignageXML.aspx?location=Student%20Center%20Cafe';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xml_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux i686; ru; rv:1.9.3a5pre) Gecko/20100526 Firefox/3.7a5pre");
$data = curl_exec($ch);
$ce = curl_error($ch);
curl_close($ch);
// this is how I was doing it prior to today and it worked before
// preg_match_all("/<MealPeriod name=\"(.+?)\">([\w\W\r\n]*?)<\/MealPeriod>/i", $data, $output_array);
// this way doesnt show all the meal periods,
// but I need to know whats in between the MealPeriod tags
// preg_match_all('/<MealPeriod name="(.*?)">(.*?)<\/MealPeriod>/i', $data, $output_array);
// shows all the meal period names,
// but I need the above to work to store whats in between the MealPeriod tags in the $output_array[2]
preg_match_all('/<MealPeriod name="(.*?)">(.*?)<\/MealPeriod>/i', $data, $output_array);
echo '<pre> '.print_r($output_array,1).'</pre>';
?>
I found the answer thanks to the following stack overflow post - php regex or | operator
I needed to change the regex to the following and I was finally able to return all the meal periods and contents there of within the correct array.
'/<MealPeriod name="(.*?)">(.*?)<\/?MealPeriod>/i'
hense the ? in <\/?Meal
I want to get information about a channel, is it online at the moment or not:
$stream_list = ...;
$mycurl = curl_init();
curl_setopt ($mycurl, CURLOPT_HEADER, 0);
curl_setopt ($mycurl, CURLOPT_RETURNTRANSFER, 1);
//Build the URL
$url = "http://api.justin.tv/api/stream/list.json?channel=" . $stream_list;
curl_setopt ($mycurl, CURLOPT_URL, $url);
$web_response = curl_exec($mycurl);
but thats always return with an empty array. I saw many example based on it - mine wont work, what am I doing wrong?
An empty array probably means nothing was found with the stream list you provided.
I used http://api.justin.tv/api/stream/list.json?channel=beyondthesummit,towelliee and was able to get an array from the API, and then I used http://api.justin.tv/api/stream/list.json?channel=foobar and got an empty JSON array back.
I'd make sure $stream_list has the value you expect. And if it does, try removing the channel filter completely to see if you get results.
It returns an empty array if the channel is not live.
Is it bad practice or will it be slower if I use curl within a foreach loop?
I'm planning on having an autocomplete input field, and the query in the input would be sent to an API call.
I'm getting an id from a certain link (ie: http://api.linke1.com/names)
foreach($json as j){
$id = $j->id; //from http://api.linke1.com/names
$url = "https://api.site/{$id}/photos";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
curl_close($ch);
$jsonDecode = json_decode($output);
$results = $jsonDecode->results;
foreach($results as $result)
{
$photoURL= $result->photo->url; //from https://api.site/{$id}/photos
}
}
So every time I type in a name, it will go into the foreach searching for an id from http://api.linke1.com/names, and then it will look for the photo url from the other link. I wanted to output a list of an array, so eventually i'll have a list of data to output showing information such as name, photo, etc...
Will this slow down dramatically because each letter typed in the input field it will run through this foreach loop. Would there be an easier way?
Thanks!
Initialize the curl and the things that doesn't change before the loop and close it afterwards.
That will speed up the thing a little bit.
and you can use curl_multi_*, which can fetch several URLs in parallel.
http://se2.php.net/manual/en/ref.curl.php
I am using couchDB to get a UUID so that I can send a new document to the database.
In order to get this UUID, I use a curl statement:
function getUUID(){
$myCurlSubmit = curl_init();
curl_setopt($myCurlSubmit, CURLOPT_URL, 'http://localhost:5984/_uuids');
curl_setopt($myCurlSubmit, CURLOPT_HEADER, 0);
$response = curl_exec($myCurlSubmit);
curl_close($myCurlSubmit);
return $response;
}
This returns the expected result:
{"uuids":["af09ffd3cf4b35c2d94d1ed755000fb8"]}
However, the following json_decode fails:
print_r('No match, creating new document.');
$uuid = json_decode(trim(getUUID()));
var_dump(json_last_error());
The error printed is: 'int(0)' (not in quotes.), and $uuid is a json string still.
Help appreciated Thank you!
EDIT:
var_dump($uuid) = int(1)
EDIT:
var_dump(getUUID()) = {"uuids":["af09ffd3cf4b35c2d94d1ed755000fb8"]}\n1
Is there any reason why I would have a trailing one, and /n on my json??
EDIT:
The problem was with curl, look at the answer below!
The problem lies in the use of curl in the getUUID() function.
You must set CURLOPT_RETURNTRANSFER, otherwise curl_exec will just echo the result, while returning 1 (as you see).
See for example this comment in the curl_exec manual: http://www.php.net/manual/de/function.curl-exec.php#13020