I've been reading this helpful post:
http://techslides.com/hacking-the-google-trends-api
It shows how you can use cURL in command line/terminal to request data from google trends, for example;
curl --data "ajax=1&cid=actors&geo=US&date=201310" http://www.google.com/trends/topcharts/trendingchart
Gives you a big block of what I think is JSON. Below is an example of what I am doing to use cURL within my PHP to get data like this- however I cannot find anything that would get the data from the above cURL command to work in PHP like below.
<?php
//initialize session
$url = "http://www.google.com/trends/hottrends/atom/feed?pn=p1";
$ch = curl_init();
//set options
curl_setopt($ch, CURLOPT_URL, $url);
//execute session
$data = curl_exec($ch);
echo $data;
//close session
curl_close($ch);
?>
How do I go about getting the data from above?
You can do the same with the PHP cURL extension. You just need to set the options through curl_setopt, so you would do something like this
$url = "http://www.google.com/trends/topcharts/trendingchart";
$fields = "ajax=1&cid=actors&geo=US&date=201310";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
Now you have the response of the website in $data and you can do whatever you want with it.
You can find the PHP cURL documentation on http://php.net/curl
Try this
// Complete url with paramters
$url = "http://www.google.com/trends/topcharts/trendingchart?ajax=1&cid=actors&geo=US&date=201310";
// Init session
$ch = curl_init();
// Set options
curl_setopt($ch, CURLOPT_URL, $url);
// Set option to return the result instead of echoing it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Execute session
$data = curl_exec($ch);
// Close session
curl_close($ch);
// Dump json decoded result
var_dump(json_decode($data, true));
Related
I am using the following code to send a CURL request to send a SMS. But the SMS is being sent twice.
$message = urlencode($message);
$smsurl = "http://$url/sendmessage.php?user=matkaon&password=$password&mobile=$mobile&message=$message&sender=$sender&type=3";
$ch = curl_init($smsurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$sentsms = curl_exec($ch);
curl_close($ch);
I tried commenting some of the lines which solved the issue but gives an output as below:
What is the proper method to send a CURL request only once?
Try this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $smsurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
Don't pass the URL as argument on the init function.
I don't know why the function is being called twice, but I never pass the URL as argument and always work great this way.
You'd usually use curl_init() with no parameters, then pass the URL into curl_exec.
Modified example 1 from curl_exec docs:
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $smsurl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
I have a Python script which gives me back some data in JSON. It starts a session, posts some auth data and requests data which comes back with JSON. That works fine, but can somebody help me to do this in PHP? I am sure it is possible but I am struggling to construct that.
import requests
with requests.Session() as s:
start_time = time.time()
s.post('http://IP:PORT/WHATEVER/AUTH', data={'username':'jdoe','password':'forgotten'})
req = 'http://IP:PORT/WHATEVER/DATA/BOM%20fe?table=cars,tires,&format=json'
res = s.get(req)
you need to look for curl request in php. here is a example
function getdata($url){
if(!function_exists("curl_init"))
die("cURL extension is not installed");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2&postvar3=value3");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
curl_close ($ch);
return $output;
}
$output = getdata("https://website.com");
var_dump($output);
you can use json decode fuction to covert into matrix and use the variables
$arr = json_decode($output,true);
you can use something like this for formatting:
curl_setopt($ch, CURLOPT_POSTFIELDS,
http_build_query(array('postvar1' => 'value1')));
I'm trying to fetch all the docs from my CouchDB hosted in Cloudant, using PHP and CURL.
So far I've tried this, I get a 200 status, but nothing on the Response column of the console.
<?php
$url = "https://myuser.cloudant.com/mydb/_all_docs?include_docs=true";
$user = 'myuser';
$pass = 'mypass';
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
return $output;
return json_decode($output,true);
curl_close($ch);
?>
I'm not fluent in PHP, what am I missing?
A couple of things look odd to me about the above code sample. I usually put the credentials in the URL so https://user:pass#myuser.cloudant.com as the URL - then you don't need the following two curl_setopt lines. I'm not exactly sure how it would work to pass those separately.
Also you're returning twice. If you just want to inspect the output try the var_dump() command and see if that shows you what you expect?
I'm trying to get some data from a website that is not mine, using this code.
<?
$text = file_get_contents("https://ninjacourses.com/explore/4/");
echo $text;
?>
However, nothing is being echo'd, and the string length is 0.
I've done this method before, and it has worked no problem, but with this website, it is not working at all.
Thanks!
I managed to get the contents using curl like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://ninjacourses.com/explore/4/");
$result = curl_exec($ch);
curl_close($ch);
cURL is a way you can hit a URL from your code to get a html response from it. cURL means client URL which allows you to connect with other URLs and use their responses in your code
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://ninjacourses.com/explore/4/");
$result = curl_exec($ch);
curl_close($ch);
i think this is useful for you curl-with-php and another
I am using the following code to get information about a repo using the Github API.
I am using cURL, but I'm not sure how to just get the name of the repository. So, how can I get just one string from the response and echo that without echoing the complete response? I tried doing $data['name'] but that didn't work.
code:
$ch = curl_init();
curl_setopt($ch,CURLOPT_USERAGENT,'Content-type: application/json');
curl_setopt($ch, CURLOPT_URL, 'https://api.github.com/repos/ruby/ruby');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
This request returns JSON, so just use json_decode.
$jsonStr = curl_exec($ch);
curl_close($ch);
$json = json_decode($jsonStr, true);
var_dump($json['name']);
var_dump($json['full_name']);
It should be trivial from here to get the elements you're interested in.