I want to grab a set of images using the URL in PHP. I've tried using file_get_contents and curl. Below is the code that I have tried using.
$image = file_get_contents('http://user:pwd#server/directory/images/image1.jpg');
file_put_contents('D:/images/image1.jpg', $image);
and
$url = 'http://server/directory/images/image1.jpg';
$localFilePath = 'D:/images/image1.jpg';
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch, CURLOPT_USERPWD, "user:pwd");
$raw = curl_exec($ch);
curl_close ($ch);
if(file_exists($localFilePath)){
unlink($localFilePath);
}
$fp = fopen($localFilePath,'wb');
fwrite($fp, $raw);
fclose($fp);
In both cases, I am getting the following error:
401 - Unauthorized : Access is denied due to invalid credentials.
The password has a special character. I can't change it to a plain password, as the password policies don't allow it.
I Don't see the place where you tell CURL to use AUTHentication:
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
I think it also might be because you're not using cookies, enable them:
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
The equivalent of this line is
curl_setopt($ch, CURLOPT_USERPWD, "user:pwd");
This one, and you can try this by replacing your one
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("Authorization: Basic ".base64_encode("user:pwd")));
// also you can try by changing + / characters into _-
Now come to the point. Base64 has several implementation based on RFC. For example : RFC3538. Different library or different language implemented different RFC for base64 encoding/decoding. For example at some implementation it uses + and / character and some use the _ and - character.
So lets say your curl is sending the base64 string for the authorization is xyz+12= but your server is expecting the string to be as xyz_12=. So it will obviously fail to decode.
Related
I am trying to add contacts to Constant Contact account using V3 API. Some user data contain accent characters, when I add these it is showing as unicode characters in constant contact account.
For example
First name is GÒKÜL and last name is NÁTH. It is showing in constant contact as Gu00d2Ku00dcL and Nu00c1TH. I want to show these as original.
I think issue is in my curl function to add/update contact. Below is the code
function updateContact($access_token,$contactid,$entry){
$ch = curl_init();
$base = 'https://api.cc.email/v3/';
$url = $base . '/contacts/'.$contactid;
curl_setopt($ch, CURLOPT_URL, $url);
$authorization = 'Authorization: Bearer ' . $access_token;
$ct = 'Content-Type: application/json;';
curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorization, $ct));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $entry);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
I tried $ct = 'Content-Type: application/json; charset=UTF-8'; and $result = utf8_decode(curl_exec($ch)); But not working.
I think someone can help me..
Those are not 'unicode' characters, they look a bit like Unicode Escape sequences, However, a backslash is missing. You should not be getting Gu00d2Ku00dcL, but G\u00d2K\u00dcL.
My hope is that those backslashes are actually there and something went wrong with sharing this output. If this is the case, the easiest way to parse these is to use the json_decode function.
If those backslases are really missing, then this suggests that the server you are working with is broken, and there's no easy fix for this. In that case you might want to contact who runs this server and let them know.
I am working on a website, that will have a large number of files. So, I made a separate server for my files such as images and txt files. The problem is that php's file_get_contents function does not work for this server.
I have tried echo file_get_contents("http://url"); and I get nothing, but when I do echo file_get_contents("http://google.com"); I get google's homepage. This the same case for a curl connection.
$ch = curl_init();
$url = "http://running-files.rf.gd/hello.html";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$body = curl_exec($ch);
$info = curl_getinfo($ch);
$error = curl_errno($ch);
curl_close($ch);
echo $body;
My guess is that there is something need in the .htaccess file. Anyone have some suggestions?
If you're opening a URI with special characters,
(such as spaces) you need to encode the URI with urlencode().
Ex:
file_get_contents("http://domain-name.com?id=".urlencode("something with special characters"));
"something with special characters" can be a variable at most cases
I am trying to fire a HTTP GET request on a secured URL which asks for username and password. This is fine when I am using that from browser but I am not sure how to do that using PHP.
I have tried using the two methods:
1) Using Curl as suggested in here: Make a HTTPS request through PHP and get response
2) Using the file_get_contents as suggested in here: How to send a GET request from PHP?
But the first one didn't give me any response back. And the second one gave me the following error:
failed to open stream: HTTP request failed
And this is my code for the curl:
$url="https://xxxxx.com";
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
and for the file_get_contents:
$url="https://xxxx.com";
$response=file_get_contents($url);
echo $response;
The URL will return a XML response for a API I am testing. Can someone point me to the right direction?
Thanks!
If we focus on the requirement to send a username and password because I suspect that's your main problem, try this
$ch = curl_init();
$url="https://xxxxx.com";
// OR - check with your server's operator
$url="http://xxxxx.com";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// or maybe
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
// - see http://stackoverflow.com/questions/4753648/problems-with-username-or-pass-with-colon-when-setting-curlopt-userpwd
// check the cURL documentation
$output = curl_exec($ch);
$info = curl_getinfo($ch);
// don't forget to check the content of $info, even a print_r($info) is better
// than nothing during debug
curl_close($ch);
I am trying to update some custom fields using the REST API and PHP/cURL.
I'm wondering if I might have edited something without realizing it, while what I have below "worked" yesterday (I think), it does not work now.
I get varying responses using the different "methods", from:
I get this one using the POST method, as it is uncommented below.
HTTP 405 - The specified HTTP method is not allowed for the requested
resource ().
I get this one if I use the commented-out PUT method, with POST commented out.
{"status-code":500,"message":"Read timed out"}
And this one mixing and matching PUT and POST.
{"errorMessages":["No content to map to Object due to end of input"]}
What am I missing/doing wrong? I am using the following code:
<?php
$username = 'username';
$password = 'password';
$url = "https://example.com/rest/api/2/issue/PROJ-827";
$ch = curl_init();
$headers = array(
'Accept: application/json',
'Content-Type: application/json'
);
$test = "This is the content of the custom field.";
$data = <<<JSON
{
"fields": {
"customfield_11334" : ["$test"]
}
}
JSON;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// Also tried, with the above two lines commented out...
// curl_setopt($ch, CURLOPT_PUT, 1);
// curl_setopt($ch, CURLOPT_INFILE, $data);
// curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result = curl_exec($ch);
$ch_error = curl_error($ch);
if ($ch_error) {
echo "cURL Error: $ch_error";
} else {
echo $result;
}
curl_close($ch);
?>
The problem here is that PHP's cURL API is not particularly intuitive.
You might think that because a POST request body is sent using the following option
that a PUT request would be done the same way:
// works for sending a POST request
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// DOES NOT work to send a PUT request
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_PUTFIELDS, $data);
Instead, to send a PUT request (with associated body data), you need the following:
// The correct way to send a PUT request
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
Note that even though you're sending a PUT request, you still have to use the CURLOPT_POSTFIELDS
option to send your PUT request body. It's a confusing and inconsistent process, but it's what you've
got if you want to use the PHP cURL bindings.
According to the relevant manual entrydocs, the CURLOPT_PUT option seems to only work for PUTting a file directly:
TRUE to HTTP PUT a file. The file to PUT must be set with CURLOPT_INFILE and CURLOPT_INFILESIZE.
A better option IMHO is to use a custom stream wrapper for HTTP client operations. This carries the
added benefit of not making your application reliant on the underlying libcurl library. Such an
implementation is beyond the scope of this question, though. Google is your friend if you're interested
in developing a stream wrapper solution.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $PathUrl);
curl_setopt($ch, CURLOPT_USERPWD, 'someuser:somepass');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
$info = curl_getinfo($ch);
Any ideas on why it works about 30% of the time and the other 70% if fails....viewing the url on any browser works all the time
You may be better off setting the Authorization header via CURLOPT_HTTPHEADER.
Eg, curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization' => 'user:pass'))
Edit: also, this may not apply because you say it works 30% of the time, but just be aware of common forms of encoding for Auth headers, eg, base64.