receive curl request - php

i am using curl in my program.
and my code is :
$tref = $_GET['tref'];
$url = "https://paypaad.bankpasargad.com/PaymentTrace";
$curl_session = curl_init($url); // Initiate CURL session -> notice: CURL should be enabled.
curl_setopt($curl_session, CURLOPT_POST, 1); // Set post method on.
//curl_setopt($curl_session, CURLOPT_FOLLOWLOCATION, 1); // Follow where ever it goes
curl_setopt($curl_session, CURLOPT_HEADER, 0); //Don't return http headers
//curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, 1); // Return the content of the call
$post_data = "tref=".$tref;
curl_setopt($curl_session, CURLOPT_POSTFIELDS, $post_data);
// Get returning data
$output = curl_exec($curl_session);
print_r($output);
print_r($post_data);
but when i use this code in my hosting , $output not set and when use another server this code correctly.
how i doing in my server.

hey found this code :
$data = curl_exec($curl_handle);
if ($data === FALSE) {
die(curl_error($curl_handle));
} else {
$html_str .= $data;
}
and when i use this code , i face this this error :
SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
then i search this error and face this link
http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
and i add this code
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
before
curl_exec():

Make sure your server curl is enable..
Change ;extension=php_curl.dll
To
extension=php_curl.dll
in php.ini you also check this Call to undefined function curl_init().?

Related

WAMP : PHP Curl "working" but returning empty string, file_get_contents is working

I am running PHP Wampserver 3.2.6 under Windows 11 with Avast Antirus Free edition.
And PHP Version 8.1.0.
Now I have setup a simple curl script to fetch data from a remote host. But this returns nothing.
When I put the entire thing online on a server it works just fine. But from a local machine it doesn't work.
I have tried running the entire thing under postman. And there it works just fine.
private function __curl($url, $decode = true){
// * create curl resource
$ch = curl_init();
// * set url
curl_setopt($ch, CURLOPT_URL, $this->api_url.$url);
// * return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// * set headers
$headers = array('X-IM-API-KEY: '.$this->api_key);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, true);
// * if any postdata set
if(!empty($this->postdata)){
// * initialize post
$this->__post();
// * set postdata
curl_setopt($ch, CURLOPT_POST, count($this->postdata));
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->postfieldstr);
}
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
// * if no decode
if(!$decode) return $output;
// * return result
return json_decode($output, true);
}
When I just use file_get_contents it works fine.
file_get_contents($this->api_url.$url);
The result :
{"success":false,"error":true,"message":"fields
missing","data":{"email":"not set","password":"not
set","app_version":"1.1"}}
Of course it will give an error because it expects POST parameters with the username and password.
I have the following configuration visible under PHPinfo :
I hope someone can tell me what my mistake would be.
EDIT
When I add :
curl_error($ch);
I get the following error :
SSL certificate problem: unable to get local issuer certificate
But when viewing the address in FireFox I get no error at all.
(letscrypt)
EDIT : Answer added by : #codenathan
Adding the following code to disable host and peer verification does the trick actually.
I think in combination with the local firewall the letscrypt certificate simply didn't get through in the way it was supposed to.
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
Since I need this for developement purposes this actually does the trick for me.
curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0);
However it is not ideal to switch this off : https://www.saotn.org/dont-turn-off-curlopt_ssl_verifypeer-fix-php-configuration/

Curl Drops Parameters

SO, I have been fighting with a piece of code that I want to use to get a remote page's source code using curl.
The code executes successfully, both in the browser and on command line. However, I get the of the main file only. When parameters are added, they are not considered whatsoever in the output.
The Code:
STACK : Ubuntu, Nginx, PHP-FPM 7.2
$urlcontent = 'https://XXX.YYY.COM/file/?var1=value1' ;
// Create a new cURL resource
$curl = curl_init();
if (!$curl) {
die("Couldn't initialize a cURL handle");
}
// Set the file URL to fetch through cURL
curl_setopt($curl, CURLOPT_URL, $urlcontent);
// Set a different user agent string (Googlebot)
curl_setopt($curl, CURLOPT_USERAGENT, 'CodiBot/2.1');
// Follow redirects, if any
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
// Fail the cURL request if response code = 400 (like 404 errors)
curl_setopt($curl, CURLOPT_FAILONERROR, true);
// Return the actual result of the curl result instead of success code
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Wait for 10 seconds to connect, set 0 to wait indefinitely
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
// Execute the cURL request for a maximum of 50 seconds
curl_setopt($curl, CURLOPT_TIMEOUT, 50);
// Do not check the SSL certificates
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// Fetch the URL and save the content in $html variable
$html = curl_exec($curl);
// Check if any error has occurred
if (curl_errno($curl))
{
echo 'cURL error: ' . curl_error($curl);
}
else
{
// cURL executed successfully
print_r(curl_getinfo($curl));
print_r($html);
}
curl_close($curl);
PROBLEM
I get the content for https://XXX.YYY.COM/file but not the corresponding ?var1=value1 part. IN other words, as I feed info to be retrieved to DB I get only the html of the main file.
I tried :
curl_setopt($ch, CURLOPT_POSTFIELDS, 'foo=1&bar=2&baz=3');
I know the remote server may have CORS enabled, but I tried the same url using a remote curl retriever and it succeeded. SO, it may not be the remote server

PHP curl post request to server using cloudflare (Full SSL) has SSL error and Blank SESSION Cookie

Hi I'm doing a website right now. Both of these files is in one server and domain and I'm using cloudflare to boost the loading. I'm using Full SSL option on cloudflare because I bought my own SSL Geotrust on my server. I already upgraded my curl on the server to 7.41.0.
One php file consist of the function
Function File:
<?php
function get_content($session){
$endpoint = "https://sample.ph/php/resource.php";
// Use one of the parameter configurations listed at the top of the post
$params = array(
"yel" => $session
);
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$endpoint);
$strCookie = 'PHPSESSID='.$_COOKIE['PHPSESSID'];
curl_setopt($curl, CURLOPT_COOKIE, $strCookie);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
$postData = "";
//This is needed to properly form post the credentials object
foreach($params as $k => $v)
{
$postData .= $k . '='.urlencode($v).'&';
}
$postData = rtrim($postData, '&');
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($curl, CURLOPT_HEADER, 0); // Don’t return the header, just the html
curl_setopt($curl, CURLOPT_CAINFO,"/home/sample/public_html/php/cacert.pem"); // Set the location of the CA-bundle
session_write_close();
$response = curl_exec($curl);
if ($response === FALSE) {
return "cURL Error: " . curl_error($curl);
}
else{
// evaluate for success response
return $response;
}
curl_close($curl);
}
?>
Resource File
<?php
session_start();
if(isset($_POST['yel'])){
$drcyt_key = dcrypt("{$_POST['yel']}");
if($drcyt_key == $_SESSION['token']){
echo "Success";
}
}
?>
How do you think will I fix this?
The SSL Verification error. Upon debugging sometimes I got cURL Error: SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
Sometimes I got cURL Error: SSL peer certificate or SSH remote key was not OK
When I put curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); to FALSE, which is not a good idea; There comes a second problem for the SESSION COOKIE becoming blank on first load.
I HOPE YOU CAN HELP ME. THANK YOU.
This issue looks to be an outdated certificate bundle or outdated OpenSSL version on the server. You should both ensure you have the latest root certificates on your computer and also ensure that you have the latest versions of OpenSSL (including the PHP OpenSSL module).

Want to use cURL instead of SimpleXML_load_file()

Using below script I can parse API data successfully.
$xml_report_daily=simplexml_load_file("https://api.sitename.com/api/reports/api_get.asp?User=00012345&Key=abcdefghijklmnop&fromDate=11/12/2014&toDate=12/12/2014&mid=25");
foreach ($xml_report_daily as $report_daily):
$trans_id=$report_daily->TRANSID;
$trans_id=$report_daily->MID;
$trans_id=$report_daily->EXT;
$trans_id=$report_daily->USER;
endforeach;
XML data are something like this:
<DATABASE>
<RECORD>
<TRANSID>1348818</TRANSID>
<MID/>
<EXT>0</EXT>
<USER>00012345</USER>
</RECORD>
.
.
.
so on...
</DATABASE>
But I want to use cURL instead of simplexml_load_file. So I used below script but it is not giving any result data.
$url = "https://api.sitename.com/api/reports/api_get.asp?User=00012345&Key=abcdefghijklmnop&fromDate=11/12/2014&toDate=12/12/2014&mid=25";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
$xml = curl_exec($ch);
echo $xml;
Please let me know what I am missing or doing wrong.
Thank you,
Ok, here is my complete answer and hope it will be useful to others.
I used 2 methods to read XML data from specific link.
Method# 1 : Using simplexml_load_file() - allow_url_fopen should be ON on hosting server for this method to work. This method is working fine both on my local as well as actual server.
$xml_report_daily=simplexml_load_file("https://api.sitename.com/api/reports/api_get.asp?User=00012345&Key=abcdefghijklmnop&fromDate=11/12/2014&toDate=12/12/2014&mid=25");
foreach ($xml_report_daily as $report_daily):
$trans_id=$report_daily->TRANSID;
$m_id=$report_daily->MID;
$ext_id=$report_daily->EXT;
$user_id=$report_daily->USER;
echo $trans_id." ".$m_id." ".$ext_id." ".$user_id."<br/>";
endforeach;
Method# 2 : Using cURL - After doing as suggested here, now this method too is working fine both on my local as well as actual server.
$url = "https://api.sitename.com/api/reports/api_get.asp?User=00012345&Key=abcdefghijklmnop&fromDate=11/12/2014&toDate=12/12/2014&mid=25";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
$xml = curl_exec($ch);
$xml_report_daily = simplexml_load_string($xml);
foreach ($xml_report_daily as $report_daily):
$trans_id=$report_daily->TRANSID;
$m_id=$report_daily->MID;
$ext_id=$report_daily->EXT;
$user_id=$report_daily->USER;
echo $trans_id." ".$m_id." ".$ext_id." ".$user_id."<br/>";
endforeach;
When using cURL, I was getting no result data so paul-crovella suggested me to check error. so I used below script and I found that I was trying to acess https (SSL certificate) data as also mentioned by Raffy Cortez
if(curl_exec($ch) === false)
{ echo 'Curl error: ' . curl_error($ch); }
else
{ echo 'Operation completed without any errors'; }
To resolve this https (SSL certificate) related issue, here is very very helpful link and you can use any of methods mentioned there as per your necessity.
HTTPS and SSL3_GET_SERVER_CERTIFICATE:certificate verify failed, CA is OK
Thank you,
You are calling https URL in your cURL, you need to use
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Unable to retrieve and save user’s display picture through cURL

I am trying to retrieve users display picture through graph api and using cUrl to save it into the disk, but am unable succeed in it and getting this error when trying to check the mime type of the picture that I saved:
Notice: exif_imagetype(): Read error! in
//$userPpicture = $user_profile[picture];
//Create image instances
$url = "http://graph.facebook.com/{$userId}/picture?type=large";
$dpImage = 'temp/' . $userId . '_dpImage_' . rand().'.jpg';
echo $dpImage;
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_content = get_data($url);
file_put_contents($dpImage, $returned_content);
echo "Type: " . exif_imagetype($dpImage);
for this updated code using curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); I am getting this error:
Warning: curl_setopt(): CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in /var/fog/apps/app12345/myapp.phpfogapp.com/start.php on line 178
If this action requires any server side configuration then i might not be able to do this as am using a shared cloud storage over phpfog.
Kindly help me with this.
Thankyou.
The graph url you are using of http://graph.facebook.com/4/picture?type=large returns a HTTP 302 redirect, not the actual user image. You would need to follow the redirect and download the image at that url which is a url that looks like this: http://profile.ak.fbcdn.net/hprofile-ak-snc4/49942_4_1525300_n.jpg
As OffBySome points out, you need to follow the 302 redirect served by graph.facebook.com to the final destination, which contains the actual image data.
The simplest way to do that in this case is to add another curl_setopt call with CURLOPT_FOLLOWLOCATION as true. i.e.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true)
Check out http://us3.php.net/manual/en/function.curl-setopt.php for more details.

Categories