PHP Curl Proxy - Is there an error? - php

$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.

Related

What is wrong with this code. it shows "msg is null"

This the code.
$data = '{"rewardTime":"20","articleTime":"0","rewardType":"copper_treasure_chest","activeDay":"17","videoTime":"20","specific":"false","userid":"2944210","version":"3","day":"2020-05-07","token":"M2U4ZjQyMWItZmNiYi00NWM4LWJhYWYtOTZhZWEwY2ExODY5"}';
$url = 'https://api.cc.clipclaps.tv/reading/obtainReward';
$curl = curl_init();
curl_setopt($curl, CURLOPT_PORT, 443);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_ENCODING, "");
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$result = json_decode($result, true);
echo "$result"."\n";
This is the result:
{"code":4009,"msg":null,"data":null,"date":1588991702039}
It should be msg: success
You would need to refer to the API's documentation to figure out what Error: 4009 is exactly.
Your code (Curl request) is working, as it is receiving a proper JSON response.. and there is no coding error that I see.
Why the API is responding with an empty result/data field, is specific to the API itself.

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

Same cURL code, different results with PHP and Laravel

When I try to save cookies without Laravel,it's saved.But with Laravel I can't find the cookie file(Searched everywhere :) ).
Here is the code(part):
...Some php code...
$dataJSON = json_encode($dataPost);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $dataJSON);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_COOKIEJAR, "cookie.txt"); //even with the realpath ,no difference
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($curl);
print_r($result);
curl_close($curl);
Is it a Routing problem?

Resource id #4 response from curl php

I am trying to post the an xml string using curl.
Here is the code
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 120);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($curl, CURLOPT_ENCODING, 'utf-8');
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'SOAPAction:""',
'Content-Type: text/xml; charset=utf-8',
));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($curl, CURLOPT_SSLCERT, '.............');
curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($curl, CURLOPT_SSLKEY, '.............');
curl_setopt($curl, CURLOPT_SSLKEYPASSWD, '');
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlData);
curl_setopt($curl, CURLOPT_HEADERFUNCTION, 'read_header'); // get header
$result = curl_exec($curl);
This is the result I keep on getting
Resource id #4
I'm new to curl but I'm trying to study it. Is that the right code in my curl php?
I expect this sample response from curl:
{"status_code":"....","message":"..........."}

Curl is enable but didnot return value

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);

Categories