PHP with cURL for POST - php

I have little function for get content result with cURL;
When i tried without POST, all working good, but with POST enabled, the result is null.
This code don't work:
function getAPI($url){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS,'shard=Apex');
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3");
$result = curl_exec($ch);
return $result;
curl_close($ch);
}
This code work but the request is not passed like a POST:
function getAPI($url){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
//curl_setopt($ch,CURLOPT_POST, true);
//curl_setopt($ch,CURLOPT_POSTFIELDS,'shard=Apex');
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3");
$result = curl_exec($ch);
return $result;
curl_close($ch);
}
I call this function with this command:
echo getAPI('http://world.needforspeed.com/SpeedAPI/ws/cmsbridge/news/rss/fr_FR');
You can also try with this URL
http://world.needforspeed.com/SpeedAPI/ws/game/nfsw/driver/levelkro/profile
This URL work only if shard=Apex (default shard=CHICANE for CHHICANE Server, but my profil is only available on Apex Server)

According to the SpeedAPI-manual (a simple Google-search) it would seem that the given URL does not accept the "shard" POST-parameter...
SpeedAPI Manual for Get News RSS Feed
You can however use a GET-request and use a shard-parameter where applicable according to the manual.
For example: http://world.needforspeed.com/SpeedAPI/ws/game/nfsw/driver/DRIVER_NAME/profile?shard=APEX (make sure you enter the shard-parameter in capitals)

Have you tried passing the parameter as an array like this instead:
curl_setopt($ch,CURLOPT_POSTFIELDS,array('shard' => 'Apex'));

Related

RSS won't parse in PHP (tried file_get_contents, curl and simplexml_load_file) [duplicate]

This question already has answers here:
PHP file_get_contents() and setting request headers
(7 answers)
Closed 3 years ago.
I'm just completely lost now, here's the URL example:
file_get_contents('http://adam-wennick.squarespace.com/actor-bro-show?format=rss');
Of course this works just fine with any other url... but this one, although it loads just fine in the browser, it returns 400 for both file_get_contents and for simplexml_load_file, while it returns 200 for curl, but the object is NULL. Has anyone of you ever encountered anything like this before?
curl code:
$rss = 'http://adam-wennick.squarespace.com/actor-bro-show?format=rss';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $rss);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$output = curl_exec($ch);
<?php
$ch = curl_init("http://adam-wennick.squarespace.com/actor-bro-show?format=rss");
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
The output is the content of the url
In case others stumble upon here - as #aynber mentioned, this URL is using some sort of scrape protection, even though it's RSS it's supposed to be scraped. :) Come on Squarespace!
As #MagnusEriksson suggested, I used file_get_contents with stream context and then replaced xml_load_file with xml_load_string:
$rss = 'http://adam-wennick.squarespace.com/actor-bro-show?format=rss';
$opts = array(
'http'=> array(
'method'=> "GET",
'user_agent'=> 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'
)
);
$context = stream_context_create($opts);
$result = file_get_contents($rss, NULL, $context);
$output = simplexml_load_string($result);
That did the trick and the $output now has the XML object. Thanks again to everyone who replied so quickly.

HTTP Request in PHP not working for specific API

I have an extremely simple script:
<?php
$jsonurl = "http://api.wipmania.com/json";
$json = file_get_contents($jsonurl);
echo $json;
?>
It works for this URL, but when I call it with this URL: https://erikberg.com/nba/standings.json
it is not echoing the data. What is the reason for this? I'm probably missing a concept here. Thanks
The problem for that particular URL is that it's expecting a different User Agent, different to the default that PHP is using with file_get_contents()
Here is a better example using CURL. It's more robust although it takes more lines of code to configure it and make it run:
// create curl resource
$ch = curl_init();
// set the URL
curl_setopt($ch, CURLOPT_URL, 'https://erikberg.com/nba/standings.json');
// Return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Fake the User Agent for this particular API endpoint
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
// $output contains the output string.
$output = curl_exec($ch);
// close curl resource to free up system resources.
curl_close($ch);
// You have your JSON response here
echo $output;

How get share count on "Viadeo" link

I want get the share count on my Viadeo link like I can do with other social API with a json file.
I have tried to do that: https://api.viadeo.com/recommend?url=http://myURL.dev but it's doesnt work. It give me some informations but not the share count.
Someone have an idea for help me to get it?
Of course it won't work on a dummy url. You should provide a valid one first. It seems it return a JSON. Just use a simple json_decode(). Example:
$url = 'https://api.viadeo.com/recommend?url=http://us.viadeo.com/en/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$results = curl_exec($ch);
$data = json_decode($results, true);
echo $data['count']; // for this example it should show
// 12
I have used the wrong item. It's work now.

PHP cURL, file_get_contents blank page

I'm trying to get a page content with cURL or file_get_content. On many websites it's working but i'm trying to do that on a friend's server and it's not.
I think there is a protection with header or things like that. I get the following error code : 401 forbidden. If i try to reach the same page with a normal browser it works.
Here is my code for the file_get_contents function :
$homepage = file_get_contents('http://192.168.1.3');
echo $homepage; // just a test to see if the page is loaded, it's not.
if (preg_match("/my regex/", $homepage)) {
// ... some code
}
I also tryed with cURL :
$url = urlencode('http://192.168.1.3');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0');
$result = curl_exec($ch) or die("Not working");
curl_close($ch);
echo $result; // not working ..
Nothing works, maybe i should add more args to curl_setopt ...
Thanks.
PS : If i try with linux (wget) i get an error, but if i try with aria2c it's working.
HTTP Status 401 means that UNAUTHORIZED. You need send the server with username and passwd。
With file_get_contents, you add the second param . That's a context-steam, which you can set header info.
You'd better to use curl for file_get_contents intend to access local file, as it's a block function. Add the option as following, it's a basic authorize.
curl_setopt($ch,CURLOPT_USERPWD,"my_username:my_password");
try this update with useragent
<?php
$curlSession = curl_init();
curl_setopt($curlSession, CURLOPT_URL, 'http://192.168.1.3/');
curl_setopt($curlSession,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);
$homepage = curl_exec($curlSession);
curl_close($curlSession);
echo $homepage ;
?>
if still getting blank page you have to install this add-on on firefox and see the "request-headers" and "response-headers"

cURL is not grabbing XML data in proper format

I'm using the following code with cURL:
function _getStatsDataXMLString($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.15) Gecko/2009101601 Firefox/3.0.15 (.NET CLR 3.5.30729)');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $url);
$xmlstr = curl_exec($curl);
curl_close($curl);
return $xmlstr;
}
The site I am trying to grab from is:
http://xml.heroesofnewerth.com/xml_requester.php?f=player_stats&opt=nick&nick[]=yanagafol
Whenever I try to request data with the above function, the string it generates is:
yanagafol173957844341541169317315000150000.009142347141111962425534613119823402602572171277264755751031151494960762160734306050303782332556000164255279111749522139194314968322760755616358407012000000011
I get the same printout with print_r
It is spaced out properly, but doesn't have any of the xml tag information so I can't convert it into an object for use with my database.
Any help would be greatly appreciated.
Try using htmlspecialchars to encode special characters in the string so that they print out as literals:
$xmlstr = htmlspecialchars(curl_exec($curl));
The inverse is htmlspecialchars_decode and htmlentities gives you more control over what gets encoded.

Categories