replace php function 'file' using curl - php

how to replace php function 'file' using curl?
$ego_quote = file($egoUrl); nicely put into an array.
$ego_quote[0] = error=OK
$ego_quote[1] = eta=Overnight
$ego_quote[2] = price=56.44
how can i achieve this when i'm using CURL?
Reason, function 'fopen' which is related to function 'file' not available on our production server.
Thank you

Thanks to Crayon Violent in this post.
$ego_quote = explode("\n",$ego_quote);
Which will give you the same result as function file
$ego_quote[0] = error=OK
$ego_quote[1] = eta=Overnight
$ego_quote[2] = price=56.44

You could do something like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $egoUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;

Related

Minifying JavaScript Effectively With Php

Hi everyone i have a Php function which use JSMin to Minify JavaScript but i want a regex to replace
long arguments name with a or b
function foo(long_arg_name, long_arg_name2){
alert(long_arg_name + long_arg_name);
}
to
function foo(a,b){
alert(a+b);
}
You can use this plugin:
https://github.com/promatik/PHP-JS-CSS-Minifier
It uses this minifier http://javascript-minifier.com/
Or use this script from
http://javascript-minifier.com/examples
<?php
// setup the URL, the JS and the form data
$url = 'http://javascript-minifier.com/raw';
$js = file_get_contents('./public/ready.js');
$data = array(
'input' => $js
);
// init the request, set some info, send it and finally close it
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$minified = curl_exec($ch);
curl_close($ch);
// output the $minified
echo $minified;
?>
Don't forget to cache everything :)

Why the curl return null?

I want to get the yahoo woeid by php script.
I can access the url 'http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text="NewYork"&format=json' and get the json file in my browser.But when I use the curl it return null.
that's the code.
public function getwoeid(){
$ch = curl_init();
$url = 'http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text="'.$this->cityname.'"&format=json';
//echo $url;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data=curl_exec($ch) or die($this->returnerror());
curl_close($ch);
$array = json_decode($data, true);
return $array;
}
when I try to access the 'http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text="上海"&format=json' the curl works fine.
I can't fix it.
any suggestion would be appreciated!
Thanks.
You can just use PHP's file_get_contents(). The spaces must be encoded by urlencode();
$url = 'http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text="'.$this->cityname.'"&format=json';
$data = file_get_contents(urlencode($url));
$array = json_decode($data, true);
Just got answer from your code:
string 'HTTP Version Not Supported
�' (length=210)
The PlaceFinder Web service supports only the HTTP GET method. Other HTTP methods are not supported.
You cannot use CURL,because curl works as a POST method.
Please read here: http://developer.yahoo.com/geo/placefinder/guide/requests.html
You have an open curl_setopt(). it is below:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ <------

Using cURL to get Facebook Album jSON with PHP

I found a script in Web Designer magazine that enables you to gather Album Data from a Facebook Fan Page, and put it on your site.
The script utilizes PHP's file_get_contents() function, which works great on my personal server, but is not allowed on the Network Solutions hosting.
In looking through their documentation, they recommended that you use a cURL session to gather the data. I have never used cURL sessions before, and so this is something of a mystery to me. Any help would be appreciated.
The code I "was" using looked like this:
<?php
$FBid = '239319006081415';
$FBpage = file_get_contents('https://graph.facebook.com/'.$FBid.'/albums');
$photoData = json_decode($FBpage);
$albumID = $photoData->data[0]->id;
$albumURL = "https://graph.facebook.com/".$albumID."/photos";
$rawAlbumData = file_get_contents("https://graph.facebook.com/".$albumID."/photos");
$photoData2 = json_decode($rawAlbumData);
$a = 0;
foreach($photoData2->data as $data) {
$photoArray[$a]["source"] = $data->source;
$photoArray[$a]["width"] = $data->width;
$photoArray[$a]["height"] = $data->height;
$a++;
}
?>
The code that I am attempting to use now looks like this:
<?php
$FBid = '239319006081415';
$FBUrl = "https://graph.facebook.com/".$FBid."/albums";
$ch = curl_init($FBUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$contents = curl_exec($ch);
curl_close($ch);
$photoData = json_decode($contents);
?>
When I try to echo or manipulate the contents of $photoData however, it's clear that it is empty.
Any thoughts?
Try removing curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); I'm not exactly sure what that does but I'm not using it and my code otherwise looks very similar. I'd also use:
json_decode($contents,true); This should put the results in an array instead of an object. I've had better luck with this approach.
Put it in the works for me category.
Try this it could work
$ch = curl_init($FBUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$contents = curl_exec($ch);
$pageData = json_decode($contents);
//object to array
$objtoarr = get_object_vars($pageData);
curl_close($ch);
Use jquery get Json instead This tips is from FB Album downloader GreaseMonkey script

Getting multiple http response codes using cURL?

Please take a look at this sample code:
function http_response($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $httpCode ;
}
this code will print the httpCode of the given url. I have couple of questions:
Can I get rid of some setopt() lines here and still getting httpCode?
What about if I want to check multiple urls at the same time? Can I modify the code to do that?
Can I do the same functionality in a simpler way using libraries different than cURL?
Thanks :)
You should be able to remove CURLOPT_HEADER and CURLOPT_NOBODY and still get the same result.
You could do that like this:
$urls = array(
'http://google.com',
'http://facebook.com'
);
$status = array();
foreach($urls as $url){
$status[$url] = http_response($url);
}
Try print_r($status); after this and you'll see the result.
You could do this with file_get_contents and $http_response_header, to learn more: http://www.php.net/manual/en/reserved.variables.httpresponseheader.php I would however recommend using cURL anyway.
*2. to check multiple urls you have to use this function in a loop, in any programming language 1 response from a server = 1 connection to that server. If you want to use 1 function to get responses from multiple servers you can always pass an array to the function and do the loop inside the function
*3. you can try this way:
function get_contents() {
file_get_contents("http://example.com");
var_dump($http_response_header);
}
get_contents();

Using urlencode in script

I've been working on a script that can be used for an internal wiki, that will remove the properties for a user who is inactive. I believe I'm almost there but having a problem with the api.
I think I need to use urlencode on the $delete path BUT only for the # in the email address and # in the property. I know how to use urlencode for the whole thing but not on just that. The way it works, is it loops through to get the properties and most of them include # in the name. Anyone who can help modify so that this works would be greatly appreciated!
Here is the script:
<?php
$user_id="john_smith#ourwiki.com";
$url=('http://admin:12345#192.168.245.133/#api/deki/users/=john_smith#ourwiki.com/properties');
$xmlString=file_get_contents($url);
$delete = "http://admin:12345#192.168.245.133/#api/deki/DELETE:users/$user_id/properties/%s";
$xml = new SimpleXMLElement($xmlString);
function curl_fetch($url,$username,$password,$method='DELETE')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // returns output as a string instead of echoing it
curl_setopt($ch,CURLOPT_USERPWD,"$username:$password"); // if your server requires basic auth do this
return curl_exec($ch);
}
foreach($xml->property as $property) {
$name = $property['name']; // the name is stored in the attribute
curl_fetch(sprintf($delete, $name),'admin','12345');
}
?>
Like this?
$delete = "http://admin:12345#192.168.245.133/#api/deki/DELETE:users/".urlencode($user_id)."/properties/%s";

Categories