How to echo the cURL result in PHP? - php

Here is a URL→ https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=snoopy&rsz=1
and this is my php code
<?php
$url = "https://ajax.googleapis.com/ajax/services/search/images?"."v=1.0&q=snoopy";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body);
?>
I wanna echo all of the image urls from its result
(For example: http://img2.wikia.nocookie.net/__cb20110331075248/peanuts/images/6/62/Snoopy.gif )
But I have no idea how to echo them.
Hope someone could help me, thanks!

Maybe like this:
<?php
$url = "https://ajax.googleapis.com/ajax/services/search/images?" .
"v=1.0&q=barack%20obama&userip=INSERT-USER-IP";
// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://example.com');
$body = curl_exec($ch);
curl_close($ch);
// now, process the JSON string
$pics = json_decode($body);
$ret='';
if($pics){
foreach($pics->responseData->results as $pic)
$ret .= $pic->url.'<br>';
}
echo $ret;
?>

in order to echo the exact result just use:
echo $body; //echoes json string
if you want to echo the exact link, echo like this:
echo $json->responseData->results[0]->url; //echoes http://img2.wikia.nocookie.net/__cb20110331075248/peanuts/images/6/62/Snoopy.gif
or you can foreach() twice (or once...) then echo $res->url directly :) your choise!

Related

PHP rest service

I'm new to PHP and i'm trying to send this of to a webservice.
<?php
$myID = "8125987";
$myVarible = "This is a test";
$service_url = "https://...?password=123&user=123&storeid=1000&mobile=$myID&message=$myVarible";
$curl = curl_init($service_url);
$result = curl_exec($curl);
curl_close($curl);
echo $result;
?>
It is not working, what is wrong?
Thanks for any help.
KHJ
1) Check if your curl function exists first
echo function_exists('curl_version') ? 1:0; // 1 = enabled , 0 =
disabled.
2) Try to access example.com instead of your target url
$url = "http://www.example.com";
3) Print out the curl info and err message before you asked a question
There're lots of CURL examples on the internet, however, I still did one for your reference.
https://www.stockeasymoney.com/ns/sit/curlRaw.php
<?php
echo function_exists('curl_version') ? 1:0; // 1 = enabled , 0 = disabled.
echo "<pre>";
$url = "http://www.example.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
print_r( curl_getinfo($ch) );
curl_close($ch);
var_dump($result);
echo "</pre>";
?>

file_get_contents phase XML

Trying to run file_get_contents from http://66.85.14.212:7254
Anyone have any suggestions how I could do this to query specific values?
I am getting the value by given code(it's working properly.):-
<?php
$url="http://66.85.14.212:7254/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($data);
echo "<pre>";
print_r($xml);
//for single variable you can write
echo $xml->Name;
?>

unable to send data using CURLOPT_POST to other php file

I am new to php , i am trying to get data from a website using curl (scraping),
UNABLE to get data from index.php to data.php, using CURLOPT_POST.. what am i doing wrong..?
followed this tutorial on youtube
index.php
<?php
$data = array("name"=>"john","age"=>31);
$string = http_build_query($data);
echo $string;
$ch = curl_init("http://localhost/scrap_practise/data.php");
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
?>
data.php
<?php
echo 'finlly in'; // this never echos
if(isset($_POST['name'],$_POST['age'])){
$db = new Mysqli("localhost","root","","mydb");
$name = $db->real_escape_string($_POST['name']);
$age = (int) $_POST['age'];
$query = "INSERT INTO data SET data='$name,$age'";
$db->query($query);
}
?>
Simply you have to update you index.php script with these line of code.
index.php
<?php
$data = array("name"=>"john","age"=>31);
$string = http_build_query($data);
echo $string;
$ch = curl_init("http://localhost/scrap_practise/data.php");
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if( ! $result = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
curl_close($ch);
// to see the return result uncomment the below line code.
//print_r($result);
?>
For more options or function reference see this link - http://php.net/manual/en/function.curl-exec.php
Hope this will help to resolve your issue !!
Try this code, I hope it will work for you...
<?php
$url = 'http://localhost/scrap_practise/data.php';
$data = array("name"=>"john","age"=>31);
$string = http_build_query($data);
echo $string;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r($result) ;
curl_close($ch);
?>
In data.php file
<?php
echo 'finlly in'; // this never echos
if(isset($_POST['name'],$_POST['age']))
{
echo "<pre>"; print_r($_POST);
}
?>
This will Output :
finlly in
Array
(
[name] => john
[age] => 31
)

Retrieve JSON using PHP from a URL

I am trying to hit an API and get some JSON back. I can’t seem to figure out what I am doing wrong here. I am getting null as the output.
Here is my PHP code:
<?php
$query_string_full = 'https://api.cityofnewyork.us/calendar/v1/search.htm?app_id=39563317lalaland&app_key=somethingsomething&categories=City%20Government%20Office';
$json = file_get_contents($query_string_full);
$obj = json_decode($json);
echo '<pre>'. json_encode($obj, JSON_PRETTY_PRINT) .'</pre>';
?>
The function file_get_contents doesn’t work with https. You should use the cURL functions instead.
<?php
$query_string_full = 'https://api.cityofnewyork.us/calendar/v1/search.htm?app_id=39563317&app_key=8396021a9bde2aad2eaf8ca9dbeca353&categories=City%20Government%20Office';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $query_string_full);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($ch);
curl_close($ch);
$obj = json_decode($json);
echo '<pre>'. json_encode($obj, JSON_PRETTY_PRINT) .'</pre>';
?>
I made this little function to return the json from a few different apis. You need to be using curl.
function exposeJSON ($apiUrl) {
$json_url = $apiUrl;
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $json_url);
// Built in authentication if needed.
//curl_setopt($ch, CURLOPT_USERPWD, "$USER:$PASS");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
// grab URL and pass it to the browser
$response = curl_exec($ch);
$return = json_decode($response[0], true);
// close cURL resource, and free up system resources
curl_close($ch);
return $return;
}
So you could do something like
$apiData = exposeJSON('urltoapi');
echo $apiData; // Should be the json.

php youtube json decode

I tried to use JSON decode to get the youtube API feed. However, when I paste the JSON result in http://www.jsonlint.com/ I noticed something like
"media$group": {
"media$category": [
Unfortunately some symbols are rejected by php. Here is my code, I tried to remove this $ symbol, but maybe not success. How do I solve this?
$url = 'http://gdata.youtube.com/feeds/api/videosq=football&orderby=published&v=2&alt=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body1 = curl_exec($ch);
$body = str_replace('$','', $body1);
curl_close($ch);
$data = json_decode($body);
foreach ($data->feed->entry as $result) {
...
}
Your problem is the usage of PHP identifiers to access the contents. The simplest solution here would be to get an array instead of an object:
$data = json_decode ( $json , $assoc = true );
This allows access to fields with:
echo $result['media$group']['media$description'];
If you want to keep the object syntax, that's possible with this kludge:
echo $result->{'media$group'}->{'media$category'};
(But arrays are safer here. You don't get a fatal error should the format change and properties be absent.)
This work:
<?php
$url = 'http://gdata.youtube.com/feeds/api/videos?q=football&orderby=published&v=2&alt=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body1 = curl_exec($ch);
$body = str_replace('$','', $body1);
curl_close($ch);
$data = json_decode($body);
foreach ($data->feed->entry as $result) {
var_dump($result);
}
?>

Categories