I'm trying to get a response using curl according to this doc. But I'm new to cURL/REST and don't have an idea how to do that.
For an example there is this code in the doc that says it returns a list of folders
curl -X GET -u "email:password" https://www.seedr.cc/rest/folder ,
but when I try it in PHP it doesn't work. how can I do this ->
Edit : it seems this is a command line curl . I tried it with command line and it works, but I need to know how to use this in a PHP script .
You don't need use curl. You can do it directly in php.
$url="https://www.seedr.cc/rest/folder"
$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode("$username:$password")
)
));
$data = file_get_contents($url, false, $context);
This is an example of how to use cURL with php. You can use the curl_setopt function to set and change the options to suit your peculiar case. Some of them are actually optional. You can check the table here to get a full list of what they do
$durl = "https://www.seedr.cc/rest/folder?email=$password" ;
$ch = curl_init() ;
curl_setopt($ch,CURLOPT_URL, $durl);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FAILONERROR, 1);
$dinf = curl_exec ($ch);
if(!curl_errno($ch) ){
echo $dinf ;
}else{
echo curl_error($ch) ;
}
Related
I'm trying to get the URL stream (.m3u8) from here:
https://chch.cdn.clearcable.net/
I tried with cURL:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://chch.cdn.clearcable.net/');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$res = curl_exec($ch);
echo $res;
However the URL stream is not present in the code.
Do I need some extra function?
Thank you in advance.
You need to tell curl to return the data instead of output it. If you use this setting:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
The $res should contain the data you expect. I can confirm this works for me. If it's not for you, try this, after thet call to curl_exec:
if($res === false) {
echo 'Curl error: ' . curl_error($ch);
} else {
echo $res;
}
When running on the web, as you mentioned, you will need to view the source to see any code that is parsed by the browser. Or you can use methods like print_r or var_dump to view it on the page.
You will need to check if its returning any value using print_r() function
Try code below
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://chch.cdn.clearcable.net/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$res = curl_exec($ch);
$post = json_decode($res ,true);
print_r($post);
curl_close($ch);
You can simply use file_get_contents() to stream the entire file into a string. But using it alone will generate an error: SSL operation failed with code 1..
This link describes why.
Try this code:
// Create a stream
$opts = [
"ssl" => [
"verify_peer" => false,
"verify_peer_name" => false
]
];
$context = stream_context_create($opts);
$res = file_get_contents("https://chch.cdn.clearcable.net/", false, $context);
echo $res;
I tried to update an existing Nextcloud user through their API. When I do it directly via shell it works
curl -u user:pass -X PUT "https://example.org/ocs/v1.php/cloud/users/admin" -H "OCS-APIRequest: true" -d key="quota" -d value="5GB"
But when I try to do it via PHP with the following code it always returns "failure 997"
$url = 'https://' . $ownAdminname . ':' . $ownAdminpassword . '#example.org/ocs/v1.php/cloud/users/admin';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$fields = array("quota" => "5GB");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'OCS-APIRequest: true'
));
$response = curl_exec($ch);
curl_close($ch);
echo "Response: ".$response;
The difference between the cURL command and the PHP code you pasted lies in a poorly designed user provisioning API.
Using these cURL arguments:
-d key="quota" -d value="5GB"
... is not equivalent to the fields you're posting:
$fields = array("quota" => "5GB");
... but rather:
$fields = array(
'key' => 'quota',
'value' => '5GB',
);
The explanation for the 997 code you're getting can be found in https://github.com/owncloud/core/blob/v10.0.3/apps/provisioning_api/lib/Users.php#L269-L272: since there's no "key" key in the data submitted ($parameters['_put']['key'] will evaluate as null) and hence the error.
I am using Php as a frontend and Java as a backend. I have created an Post API for uploading file and using curl for api request.
I have hit my Api using Postman at that time it works fine but i am facing prodblem when i request api using Curl i don't eble to get what i am doing wrong.
Here is the curl requested data :-
$data2 = array(
'file' =>
'#' . $data1->file->tmp_name
. ';filename=' . $data1->file->name
. ';type=' . $data1->file->type
);
This is how i am sending curl request:-
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch,CURLOPT_URL,$this->url);
curl_setopt($ch, CURLOPT_HEADER, 1); //parveen
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); //parveen
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data2);
$headers = array(
'Content-Type:'.$this->service->contentType,
'Launcher:'.$this->serverName,
'domain:'.$this->service->domain,
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$this->responseBody=curl_exec($ch);
Links where i find this solution:-
enter link description here
I search a lot to find the solution but nothing is worked for me so please help me .
Thanks
the way you're trying to upload the file hasn't been supported since the PHP5 days, and even in 5.5+ you'd need CURLOPT_SAFE_UPLOAD to upload with #. use CURLFile when uploading files, like
$data2 = array(
'file' => new CURLFile($data1->file->name,$data1->file->type,$data1->file->tmp_name)
);
also, don't use CURLOPT_CUSTOMREQUEST for POST requests, just use CURLOPT_POST. (this is also true for GET requests and CURLOPT_HTTPGET )
also, check the return value of curl_setopt, if there was a problem setting your option, it returns bool(false), in which case you should use curl_error() to extract the error message. use something like
function ecurl_setopt($ch,int $option,$value){
if(!curl_setopt($ch,$option,$value)){
throw new \RuntimeException('curl_setopt failed! '.curl_error($ch));
}
}
and protip, whenever you're debugging curl code, use CURLOPT_VERBOSE, it prints lots of useful debugging info
I am trying to access the cdnify API to purge cache for an individual file ( https://cdnify.com/learn/api#purgecache )
This is my current code
$cdn_api_user = env('CDNIFY_API');
$cdn_api_password = env('CDNIFY_API_PASS');
$cdn_api_resource = env('CDNIFY_API_RESOURCE');
$cdnifyapicacheurl = 'https://' . $cdn_api_user . ':' . $cdn_api_password . '#' . 'cdnify.com/api/v1/resources/' . $cdn_api_resource . '/cache';
return print $cdnifyapicacheurl;
$fields = array(
'files' => $storageFilename
);
$fields_string = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $cdnifyapicacheurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
//unless you have installed root CAs you can't verify the remote server's certificate. Disable checking if this is suitable for your application
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//perform the HTTP DELETE
$result = curl_exec($ch);
//close connection
curl_close($ch);
the env variables at the top call in my api key, password, and resource for the url. I have verified I am logging in via that url.
When I debug through my code i get an error on
$fields = array(
'files' => $storageFilename
);
which is Array to string conversion.
The $storageFilename variable returns
$storageFilename = "/" . $directoryname . "/" . $asset->name;
which is the filename required for the API call of DELETE.
I can't get passed that $fields array. The other stuff below it may or may not run properly. I am just stuck on how to write this part out.
CURLOPT_POST is just there to indicate if some post data should be included in the HTTP request, so its value should a boolean (true or false).
If your array $fields represents the data to be posted, you need to use http_build_query() to assign them to CURLOPT_POSTFIELDS:
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
There is a return in your code that stops the code the curl code is not getting executed remove it or comment it using // and try again and CURLOPT_POST value is boolean true or false that indicates if you want to use post method or not, your CURL code is really messed up you want to use http delete method or post method ?? You can only use one method, please learn how to use php cURL first http://php.net/manual/en/book.curl.php
I read over 20 related questions on this site, searched in Google but no use. I'm new to PHP and am using PHP Simple HTML DOM Parser to fetch a URL. While this script works with local test pages, it just won't work with the URL that I need the script for.
Here is the code that I wrote for this, following an example file that came with the PHP Simple DOM parser library:
<?php
include('simple_html_dom.php');
$html = file_get_html('http://www.farmersagent.com/Results.aspx?isa=1&name=A&csz=AL');
foreach($html->find('li.name ul#generalListing') as $e)
echo $e->plaintext;
?>
And this is the error message that I get:
Warning: file_get_contents(http://www.farmersagent.com/Results.aspx?isa=1&name=A&csz=AL) [function.file-get-contents]: failed to open stream: Redirection limit reached, aborting in /home/content/html/website.in/test/simple_html_dom.php on line 70
Please guide me what should be done to make it work. I'm new so please suggest a way that is simple. While reading other questions and their answers on this site, I tried cURL method to create a handle but I failed to make it work. The cURL method that I tried keeps returning "Resources" or "Objects". I don't know how to pass that to Simple HTML DOM Parser to make $html->find() work properly.
Please help!
Thanks!
Had a similar problem today. I was using CURL and it wasn't returning my any error. Tested with file_get_contents() and I got...
failed to open stream: Redirection limit reached, aborting in
Made a few searches and I'v ended with this function that works on my case...
function getPage ($url) {
$useragent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36';
$timeout= 120;
$dir = dirname(__FILE__);
$cookie_file = $dir . '/cookies/' . md5($_SERVER['REMOTE_ADDR']) . '.txt';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_ENCODING, "" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_AUTOREFERER, true );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com/');
$content = curl_exec($ch);
if(curl_errno($ch))
{
echo 'error:' . curl_error($ch);
}
else
{
return $content;
}
curl_close($ch);
}
The website was checking for a valid user agent and for cookies.
The cookie issue was causing it! :)
Peace!
Resolved with:
<?php
$context = stream_context_create(
array(
'http' => array(
'max_redirects' => 101
)
)
);
$content = file_get_contents('http://example.org/', false, $context);
?>
You can also inform if you have a proxy in the middle:
$aContext = array('http'=>array('proxy'=>$proxy,'request_fulluri'=>true));
$cxContext = stream_context_create($aContext);
More details on: https://cweiske.de/tagebuch/php-redirection-limit-reached.htm (thanks #jqpATs2w)
Using cURL you would need to have the CURLOPT_RETURNTRANSFER option set to true in order to return the body of the request with call to curl_exec like this:
$url = 'http://www.farmersagent.com/Results.aspx?isa=1&name=A&csz=AL';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// you may set this options if you need to follow redirects. Though I didn't get any in your case
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$content = curl_exec($curl);
curl_close($curl);
$html = str_get_html($content);
I also needed to add this HTTP context options ignore_errors :
see : https://www.php.net/manual/en/context.http.php
$arrContextOptions = array(
"ssl" => array(
// skip error "Failed to enable crypto" + "SSL operation failed with code 1."
"verify_peer" => false,
"verify_peer_name" => false,
),
// skyp error "failed to open stream: operation failed" + "Redirection limit reached"
'http' => array(
'max_redirects' => 101,
'ignore_errors' => '1'
),
);
$file = file_get_contents($file_url, false, stream_context_create($arrContextOptions));
Obviously, I only use it for quick debugging purpose on my local environment. It is not for production.
I'm not sure exactly why you redefined the $html object with a string from get html, The object is meant to be used for searching the string. If you overwrite the object with a string, the object no longer exists and cannot be used.
In any case, to search the string returned from curl.
<?php
$url = 'http://www.example.com/Results.aspx?isa=1&name=A&csz=AL';
include('simple_html_dom.php');
# create object
$html = new simple_html_dom();
#### CURL BLOCK ####
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
# you may set this options if you need to follow redirects.
# Though I didn't get any in your case
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$content = curl_exec($curl);
curl_close($curl);
# note the variable change.
$string = str_get_html($content);
# load the curl string into the object.
$html->load($string);
#### END CURL BLOCK ####
# without the curl block above you would just use this.
$html->load_file($url);
# choose the tag to find, you're not looking for attributes here.
$html->find('a');
# this is looking for anchor tags in the given string.
# you output the attributes contents using the name of the attribute.
echo $html->href;
?>
you might be searching a different tag, the method is the same
# just outputting a different tag attribute
echo $html->class;
echo $html->id;