Curl is enable but didnot return value - php

I have installed curl. and configuration on my system is
cURL support enabled
cURL Information 7.35.0
Age 3
I have write code in my project is :
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
curl_setopt($curl, CURLOPT_SSLVERSION,6);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($curl, CURLOPT_URL, $submit_url);
$curl_execute = curl_exec($curl);
curl_close($curl);
Its not giving me value on my local machine while working fine on server. What is problem?
My PHP version is
5.5.9-1ubuntu4.5
I am using ubuntu 13.10
what is problem with my local machine? what i am missing. please suggest what to do?
Edit :
If i missed any information to write, please inform me.. i am using curl very first time.

At first define the URL and post values correctly, I have added examples, please try this:
// define url examples
$submit_url = 'http://localhost/phptest/service.php';
// post fields examples
$postfields = 'name=foo&pass=bar&format=json';
// Start the process:
$curl = curl_init();
// Tell cURL to fail if an error occurs:
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
// Allow for redirects:
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
// Assign the returned data to a variable:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Set the timeout:
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
// Use POST:
curl_setopt($curl, CURLOPT_POST, 1);
// Set the POST data:
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curl, CURLOPT_URL, $submit_url);
// Execute the transaction:
$result = curl_exec($curl);
// Close the connection:
curl_close($curl);
// Print the results:
print_r($result);

Related

How to make website authorization using curl and php?

I'm trying to realize a simple authorization using login and password. I get a cookie but not sure if authorized or not (how to check?), also I am trying to redirect after authorization but as a result, I get Error 404 after my code tried to redirect (followlocation, true).
Here's my php code
Trying just to redirect but 404 occurred.
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($curl);
echo $result;
curl_close($curl);
And here's trying to authorize, not sure if it works (but I get cookie and in header I get 200 OK)
$loginData = [
urlencode('j_username') => 'someName',
urlencode('j_password') => 'somePassword'
];
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 2);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($loginData));
curl_setopt($curl, CURLOPT_COOKIEJAR, $_SERVER['DOCUMENT_ROOT'].'/authCookie.txt');
curl_setopt($curl, CURLOPT_COOKIEFILE, $_SERVER['DOCUMENT_ROOT'].'/authCookie.txt');
$result = curl_exec($curl);
echo $result;
curl_close($curl);
Also, I use urlencode but not sure if I use it in the right way (I have it in curl like $curl --location --request POST 'http://someFullName' \ --data-urlencode 'j_username=someUsername' \ --data-urlencode 'j_password=somePassword')
for auth you can use http basic auth it very simple in use for client and server, client code in this case will be look like:
<?php
const COOKIE_FILE = '/tmp/authCookie';
$host = 'localhost';
$username = 'login';
$password = 'pass';
$ch = curl_init($host);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERPWD, sprintf('%s:%s', $username, $password));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
$return = curl_exec($ch);
curl_close($ch);
about your 4xx error, i think you have problems with server, for test redirections you can do something like:
<?php
//stable route with redirect
$host = 'https://bitly.is/EnterpriseButton';
$userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:76.0) Gecko/20100101 Firefox/76.0';
$curl = curl_init($host);
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $host);
//with enabled followlocation
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_HEADER, true);
$result = curl_exec($curl);
curl_close($curl);
var_dump($result);
$curl = curl_init($host);
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $host);
//with disabled followlocation
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_HEADER, true);
$result = curl_exec($curl);
curl_close($curl);
var_dump($result);
and for check auth work you can auth through browser with cookies from curl

why my php curl doesn't work when CURLOPT_SSL_VERIFYHOST = 0?

environment:
OS:macOS
PHP:7.1.30
curl:7.65.0
openssl:1.0.2r
I've been trying to call https api with php curl library,it return 403 http code on my machine but 200 on my workmate's Mac.
this is the output html image
my curl configure code:
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($curl, CURLOPT_SSLVERSION, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64)');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
the configure code above works on my workmate's Mac.
But if I set CURLOPT_SSL_VERIFYHOST = 1 or 2, it works too.
Has anyone been in this situation?

Error using curl php, getting http status 500

Hello stackoverflow community, I am having some problems when I try to send a request with curl in php, but something odd is that I have 2 servers, and when I use my code in one of them I get the response Im looking for, but in the other one I get the following response:
Http status code 500
As you can see it says "HTTP Status 500 - empty String", but why is that? in the other server I am not getting that with the same code
Thanks in advice to anyone who can help me out!
Ok mistake by no posting my code, here it is
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_REFERER, $_SERVER['HTTP_HOST']);
curl_setopt($curl, CURLOPT_POST, 1);
//curl_setopt($curl, CURLOPT_PORT, 443);
curl_setopt($curl, CURLOPT_POSTFIELDS, $string_data);
curl_setopt($curl, CURLOPT_TIMEOUT, 300);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);

PHP Curl Proxy - Is there an error?

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl,CURLOPT_HEADER,0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl,CURLOPT_PROXY,'103.228.152.11');
curl_setopt($curl,CURLOPT_PROXYPORT,'80');
curl_setopt($curl,CURLOPT_USERAGENT,"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
if($post){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,$post);
}
$result = curl_exec($curl);
curl_close($curl);
$arr = array("\t" => null,"\n" => null,"\r" => null);
return strtr($result,$arr);
I was pulling data from a site, but I can not pull it. I am using curl proxy for the first time, is there an error in the above code? Because he does not pull it again.

PHP - CURLOPT_PROXYPORT issue

I am so confused about my cURL script requesting the target page "facebook.com" for example, with the same port as the proxy.
example:
proxy is: 178.215.111.70:9999 . a cURL error says:
Failed connect to facebook.com:9999; Connection timed out
I see that it tries to connect to facebook using the poxy's port: 9999
Here is my code:
<?php
$curl = curl_init("https://facebook.com");
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_REFERER, 'https://www.facebook.com');
if ($var) {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, "test");
}
curl_setopt($curl, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($curl, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT ,9999999);
curl_setopt($curl, CURLOPT_TIMEOUT, 0);
curl_setopt($curl, CURLOPT_PROXY, '178.215.111.70');
curl_setopt($curl, CURLOPT_PROXYPORT, '9999');
$result['exe'] = curl_exec($curl);
$result['err'] = curl_error($curl);
curl_close($curl);
echo $result['err'];
echo $result['exe'];
?>
I solved my problem.
It was a problem in the server only. When i tried the SAME script in another server it worked 100% fine
So always make sure of the configuration in your server when you face a problem like this.

Categories