getting no errors with curl and also dont get any response - php

am trying to login to netflix using php curl but i get no response at all or errors.here is my code
$handle = curl_init();
$url = "https://www.netflix.com/ke-en/login";
// Array with the fields names and values.
// The field names should match the field names in the form.
$postData = array(
'userLoginId' => 'Lady',
'password' => 'Gaga',
'submit' => 'ok'
);
curl_setopt_array($handle,
array(
CURLOPT_URL => $url,
// Enable the post response.
CURLOPT_POST => true,
// The data to transfer with the response.
CURLOPT_POSTFIELDS => $postData,
CURLOPT_RETURNTRANSFER => true,
)
);
$data = curl_exec($handle);
echo curl_error($handle);
echo curl_errno($handle);
curl_close($handle);
echo $data;
the error i get is 0 and i saw that it means it is successfull

Related

Codeigniter to Send form data to api url

I have form (5 fields which includes 1 file attachment field), whose field's data I need to submit on db as well as send on api url.
If I put api url in action of form tag then form data being send successfully. BUT I can't add api url in action coz like this form will redirect to that api url by leaving the site.
So to avoid putting api url in action I used following code to send form data to api url.
/* Endpoint */
$url = 'http://url_here';
$tmpfile = $_FILES['image']['tmp_name'];
$filename = basename($_FILES['image']['name']);
$data = array(
'to' => $to,
'cc' => '',
'message' => $subject,
'attachment' => '#'.$tmpfile.';filename='.$filename,
'message' => $message
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
/* Define content type */
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
/* Return json */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
/* make request */
$result = curl_exec($ch);
/* close curl */
curl_close($ch);
echo $result;
This code giving this error.
{"timestamp":"2022-03-09T07:23:54.873+00:00","status":415,"error":"Unsupported Media Type","message":"","path":"/QRCodeGenerator/sendmail"}
While searching for solution, I also tried adding
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:application/json'));
For Accept:application/json to work I did convert array into json using jason_encode(). But it was still giving same error.
Where I am getting wrong?
UPDATE
I tried this code also which I got from postman where api working fine.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://url_here/QRCodeGenerator/sendmail',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('to' => 'example#gmail.com','cc' => '','message' => 'This is test subject','body' => 'this is body msg','attachment'=> new CURLFILE('/path/to/file')),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
But this is giving blank page. $response printing nothing.

Twilio Verify API: "Requested URL was not found"

I am using curl to perform a request to the Twilio Verify API, following the instructions here: https://www.twilio.com/verify/api
Using these instructions, I've created two php files to perform the curl request -- one to get a verification code (get_code.php), and another to check the verification code (check_code.php). These scripts are called using an ajax post to send the parameters, and the two scripts are nearly identical, save for the URL ("/start" vs. "/check").
I believe I am specifying the correct URLs, and get_code.php works, but check_code.php throws the following error:
Requested URL was not found. Please check http://docs.authy.com/ to see the valid URLs.
get_code.php
<?php
$USER_PHONE = htmlspecialchars($_POST["phone"]);
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "https://api.authy.com/protected/json/phones/verification/start",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'country_code' => '1',
'via' => 'sms',
'phone_number' => $USER_PHONE,
),
CURLOPT_HTTPHEADER => array('X-Authy-API-Key: MY_KEY')
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
check_code.php
<?php
$USER_PHONE = htmlspecialchars($_POST["phone"]);
$VERIFY_CODE = htmlspecialchars($_POST["code"]);
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "https://api.authy.com/protected/json/phones/verification/check",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'country_code' => '1',
'phone_number' => $USER_PHONE,
'verification_code' => $VERIFY_CODE
),
CURLOPT_HTTPHEADER => array('X-Authy-API-Key: MY_KEY')
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
I performed a curl manually in terminal using the same URL and parameters, and it worked.
curl "https://api.authy.com/protected/json/phones/verification/check?phone_number=MY_PHONE&country_code=1&verification_code=MY_CODE" -H "X-Authy-API-Key: MY_KEY"
I don't know what I could be doing wrong?
OK, well I have no idea why this worked, but I got it working and maybe someone else can explain why. I built the CURL URL as a string and I removed the CURLOPT_RETURNTRANSFER and CURLOPT_POST arguments.
<?php
$USER_COUNTRY = "1";
$USER_PHONE = htmlspecialchars($_POST["phone"]);
$VERIFY_CODE = htmlspecialchars($_POST["code"]);
$URL = "https://api.authy.com/protected/json/phones/verification/check?country_code=1&phone_number=".$USER_PHONE."&verification_code=".$VERIFY_CODE;
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => $URL,
CURLOPT_HTTPHEADER => array('X-Authy-API-Key: MY_KEY')
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
🤷

HTTP Error 400. The request is badly formed in cURL

Here is my code.
<?php
if(isset($_REQUEST['name']) and ($_REQUEST['email']) and ($_REQUEST['msg'])){
$name=$_REQUEST['name'];
$email= $_REQUEST['email'];
$msg = $_REQUEST['msg'];
$url="http://14.140.111.4:20002/ContactUs?name=$name&Email=$email&Message=$msg";
$url = urlencode($url);
$curl = curl_init();
// Set some options - we are passing in a useragent too here urlencode
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Cubewires Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'name'=>$name,
'Email' => $email,
'Message'=>$msg
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
print_r($resp);
// Close request to clear up some resources
curl_close($curl);
}
?>
i got response HTTP Error 400. while using this API data should be inserted in database.
Thanks,
vivek

eway direct payment curl code not working

I am trying to charge the customer using eway payment method. I am implementing the procedure through curl and here is the code to the function.
public function testing_direct() {
$url = 'https://api.sandbox.ewaypayments.com/DirectPayment.json'; // PROD
$postData = array(
'Method' => 'TokenPayment',
'TransactionType' => 'Recurring',
'TokenCustomerID' => '918549937032',
'TotalAmount' => '1995',
'InvoiceNumber' => '123',
'InvoiceDescription' => 'testing'
);
$ch = curl_init($url);
$api_authentication = base64_encode('####;****');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json','Authorization:####'
),
CURLOPT_POSTFIELDS => json_encode($postData)
));
$response = curl_exec($ch);
// Check for errors
if ($response === FALSE) {
die(curl_error($ch));
}
$responseData = json_decode($response, TRUE);
print_r($responseData);
die;
}
This code doesnot print anything at all...Nothing in the error log either...any ideas that why it is not working??
Thanks
There are a few things that need to be fix to get that script working, particularly around the authentication which is the most immediate cause of a problem.
There needs to be a check of the HTTP status of the request since curl_exec only returns FALSE if there is an error - e.g. a 404 (page not found) or 401 (unauthorized) is still treated as a successful request.
// Check for errors
if ($response === FALSE) {
die(curl_error($ch));
}
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code != 200) {
die('HTTP error: '.$code);
}
The actual authentication appears to suffer from 2 main issues: there is a semicolon instead of a colon between the username (API key) and password, and the Basic part of the authentication is missing:
$api_authentication = base64_encode('####:****');
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json','Authorization: Basic '.$api_authentication
),
This could be simplified by just using CURLOPT_USERPWD:
CURLOPT_USERPWD => '####:****',
Apart from that, note that the $postData array needs some extra nesting: TokenCustomerID should be a child of Customer and TotalAmount and the Invoice fields should be children of Payment:
$postData = array(
'Method' => 'TokenPayment',
'TransactionType' => 'Recurring',
'Customer' => array(
'TokenCustomerID' => '911058757297',
),
'Payment' => array(
'TotalAmount' => '1900',
'InvoiceNumber' => '123',
'InvoiceDescription' => 'testing',
),
);
Check out the example in the documentation for a complete example request.
Good luck!

get data from database using curl and json

Do any of you know a way to get datas from a database of a website? I already studied and made a program that POST data using json. It gets the shipment status of a certain tracking number and if it's shipped, it will be changed to delivered. And I already done that.
Now what I'm trying to do is get all the datas, or the row of that tracking number and then encode it to JSON. But I don't know what to do.
This is how I did the first program: on getting a specific tracking number and update the status to delivered.
<?php
$url='https://sample.com.ph';
$apikey='apikey';
$method ='SendPackageStatus';
$data = array(
'package' => array(
'tracking_number' => '735898532',
'package_status' => 'delivered',
'failed_reason' => '',
'updated_at' => '2014-01-01 07:02:14'
)
);
$check=json_encode($data);
$postdata = "&data=$check&apikey=$apikey&method=$method";
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_URL => $url.'/webservice/',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_VERBOSE => true,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postdata,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => array('Content-type: application/x-www-form-urlencoded',
)
)
);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
Why not use mysql?... you can store json string on db (as..strings). I dont follow what you are trying to do.

Categories