Using cURL to read JSON data - php

Using Chrome to inspect the url: http://www.wizards.com/Magic/PlaneswalkerPoints/76954206# I am able to see the url where the JSON data is grabbed from:
Request URL:http://www.wizards.com/Magic/PlaneswalkerPoints/JavaScript/GetPointsSummary/76954206
Running my code:
$user_agent = 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36';
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Fail on errors
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // times out after 5s
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($json)));
curl_setopt($ch, CURLOPT_POST, 1);
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
// further processing ....
if ($server_output == "OK") {
print_r($ch);
} else {
echo "Boo";
}
Leaves me with a blank page. It's not returning JSON, only the redirect error page.
Is there a certain call I am missing to grab the JSON data?

Try this:
<?php
$url = 'http://www.wizards.com/Magic/PlaneswalkerPoints/JavaScript/GetPointsSummary/76954206';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 0);
curl_setopt($ch,CURLOPT_POSTFIELDS, '');
$result = curl_exec($ch);
curl_close($ch);
echo $result;
Output:
{"Result":true,"SessionExpired":false,"Data":[{"Key":"LifetimePoints","Value":6016,"ReplaceElement...

Related

How to read json file from url in php/laravel?

I want to open a JSON from url in php/laravel file. this is my code :
{{ini_set("allow_url_fopen", 1)}}
{{$id_ = $blog_post->featured_media}}
{{$url_ = 'http://example.net/blog/wp-json/wp/v2/media/'.$id_}}
{{$data = #file_get_contents($url_)}}
{{$json = #json_decode($data, true)}}
{{var_dump(#$json)}}
when i try reload page i get this error :
something went wrong
how can i read JSON from url ?
Use cURL to get the json data. Like this
$url = 'www.yoururl.com/full-url';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result= curl_exec ($ch);
curl_close ($ch);
$info = json_decode($result, true);
print_r($info); // print all data

Getting GotoMeeting access token using PHP CURL

I am creating a Wordpress Plugin that would create a meeting after some transaction but I don't know how to get the token. I made some tests but doesn't seem to work.
Tried doing some post CURL.
$consumerKey = "consumer_key_here";
$url = "https://api.getgo.com/oauth/v2/authorize?client_id={$consumerKey}&response_type=code";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
$test = curl_exec($ch);
if (curl_error($ch)) {
echo curl_error($ch);
}
print $test;
This should be redirected to the desired redirect url with a response code along the parameter.
To encode these values, open an encoding site and paste in the consumerKey, add a colon (:), and then paste in the consumerSecret. No spaces, no quotes, no brackets. Submit the values and an encoded value is returned that will look something like: ZXhhbXBsZV9jbGllbnRfaWQ6ZXhhbXBsZV9jbGllbnRfc2VjcmV0==.
$ch = curl_init();
if($ch) {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$token="for this you need to use documentation ";
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded','Authorization: Basic '.$token,'Accept:application/json'));
$curlulr="https://api.getgo.com/oauth/v2/token";
curl_setopt($ch, CURLOPT_URL, $curlulr);
curl_setopt($ch, CURLOPT_POST, true);
$data = 'grant_type=authorization_code'.
'&code={responseKey}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$rmv_results = curl_exec($ch);
if($rmv_results === false) {
echo('Error performing rmv lookup: '.curl_errno($ch).' - '.curl_error($ch).' (Message ID: '.basename(__FILE__).'-'.__LINE__.')');
} // end if errors
// echo "3";
curl_close($ch);
print_r($rmv_results);
echo ">><br>". $rmv_results;
}

How to login on Discogs.com using PHP curl (or other)?

I'm trying to login on website http://www.discogs.com using PHP script and cURL.
I need that to get data from https://www.discogs.com/sell/mywants?ev=wsim - it's a list of offers with items added to want-list. From what I found this list is not available in API.
I have problem with logging in using standard cURL request. Here's my code:
$username = 'myuser';
$password = 'mypassword';
$loginUrl = 'https://www.discogs.com/login';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username='.urlencode($username).'&password='.urlencode($password));
curl_setopt($ch, CURLOPT_COOKIEJAR, 'c:\cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'c:\tmp.txt');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
if (curl_error($ch)) {
echo curl_error($ch);
return;
} else {
echo "OK";
}
curl_setopt($ch, CURLOPT_URL, 'https://www.discogs.com/sell/mywants?ev=wsim');
$content = curl_exec($ch);
echo $content;
curl_close($ch);
As a result I get login page instead of page with listed offers.
Could you tell me what am I doing wrong?
As I tested, the login page requires posting another field, which is Action.Login in order to make the login request valid.
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username='.urlencode($username).'&password='.urlencode($password).'&Action.Login=');
Hope this helps.

JSON for mobile

I have script for video player
<?php
$id = 35719350987;
$url = 'http://ok.ru/dk?cmd=videoPlayerMetadata&mid='.$id;
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIE, 'viewport=1040; _flashVersion=1');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Accept: *'));
curl_setopt($ch, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_REFERER, 'ok.ru/videoembed/'.$id);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
#curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$html = curl_exec($ch);
$json = json_decode($html);
?>
The output of browser chrome is
callbackFunc([ {file:"........"video/mp4"}])
Is work
But when i use mobile is not work
When from mobile json need to take [{"name":"mobile","url":"....} , from
http://www.ok.ru/dk?cmd=videoPlayerMetadata&mid=35719350987 but is not take nothing
This line does not work curl_setopt($ch, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']); instead curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.3 Mobile'); but it does not work in mobile browsers

Why do I get a white screen when I use my cURL with a proxy?

Why isn't my cURL working with a proxy? I have tried using several proxies and it all it ever does is give me a white screen.
Here is the PHP code:
function curl($url){ // Defining the basic cURL function
$proxy = '122.166.5.84:8080';
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy_userpass);
$data = curl_exec($ch);
if($data === false || curl_error($ch)) {
return $data;
}else{
curl_close($ch);
return $data;
}
}

Categories