PHP cURL different result with cuRL in bash command line - php

I try to get html result from a site using cURL in PHP, and I got 301 moved permanently, But when I use command line, it working and returning html string reuslt.
This is my Code PHP (result not success)
curl_setopt_array($curl, array(
CURLOPT_URL => "https://detail.1688.com/offer/535394058500.html?spm=b26110380.sw1688.mof001.302.6YIDM0&v=1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "a=b",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded",
"postman-token: b60107e9-4350-e93f-d6cf-d626505687b8"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response; // return 301 moved permanent
}
And this is in CMD (result success)
curl -X GET 'https://detail.1688.com/offer/535394058500.html?spm=b26110380.sw1688.mof001.302.6YIDM0' -H 'cache-control: no-cache' -H 'content-type: application/x-www-form-urlencoded' -H 'postman-token: 2b11da6f-dcde-d527-289c-bfbf8a85d30c'

Related

Determine the file type downloaded by cURL

I have this code,
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://example.com/v1/items/id/preview",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Authorization: Bearer bearer-string",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
file_put_contents('file.jpeg', print_r($response, true));
}
From that code I can tell that the response is jpeg that's why I assigned file.jpeg as filename.
Now my problem, what if the response is not jpeg?
I could not find any function from https://www.php.net/manual/en/ref.filesystem.php that will dynamically determine what type of file is in response.
Any Idea about this.
You can use curl_getinfo to get content-type of what curl got
$mime = curl_getinfo(response , CURLINFO_CONTENT_TYPE);
and then simply check if $mime equals to 'image/jpeg'

How to upload video using put.re api (using CURL)

I want to upload videos to put.re, a file hosting provider using php curl
I tried this code:
foreach ($_FILES['uploadvid']['tmp_name'] as $index => $fileTmpName) {
$fileName = $_FILES['uploadvid']['name'];
$size = $_FILES['uploadvid']['size'];
$handle = fopen($fileTmpName, "r");
$data = fread($handle, filesize($fileTmpName));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_URL => "https://api.put.re/upload",
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_TIMEOUT => 0,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array( 'file' => # $data),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$pms = json_decode($response, true);
$vidurl = $pms['data']['link'];
if ($vidurl!="") {
echo 'Success';
} else {
echo 'Problem';
echo $err;
echo $response;
}
}
But this echo Problem.
If you check the api docs, you will see there is no output for error.
You can check The Api Docs here . There is no example shown in it's site.
The $err returns nothing,
the $reponse returns a message: NO files(s) found.
I think there is a mistake in the API call...
Please Help me to get through this.
Please NOTE that, I want to upload videos, not images. put.re allows any kind of file to be uploaded. I tried to upload files less than 100mb (which is a limit)
Can you share more detail about the error you getting ? For example i tried the api with code at below and get response which says upload disabled.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.put.re/upload",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"example.png\"\r\nContent-Type: image/png\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Accept-Encoding: gzip, deflate",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Content-Length: 20895",
"Content-Type: application/x-www-form-urlencoded",
"Host: api.put.re",
"User-Agent: PostmanRuntime/7.20.1",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

How to perform a file upload via CURL and PHP

I have been creating a script to insert data from a JSON file to a POST request.
While it does what it needs to download the image in the URL supplied than it needs to download it and perform a post request on my route.
I have been struggling for the past few hours to get it working but no success.
I have used realpath and CurlFile but it didn't make it work.
Here is the request:
<?php
$image = $this->makeCurlFile(realpath("scorpion_stretch-59788999e4432d80.jpg"));
$body = ["name" => "uploaded_file", "filename" => $image->name];
echo print_r($body);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://resources.diamondappgroup.com/upload.php",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Content-Type: application/x-www-form-urlencoded",
"Host: resources.diamondappgroup.com",
"Postman-Token: 45ebd192-9621-482c-8735-2061bc31c6fa,6a7a959f-372d-4afb-9efa-6b4da47ea1b0",
"User-Agent: PostmanRuntime/7.11.0",
"accept-encoding: gzip, deflate",
"cache-control: no-cache",
"content-length: 312659",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

Error executing curl from php

The following code runs perfectly using POSTMAN. While I try to use the code generated by postman for PHP, it gives me errors.
Original Code:
curl -X POST \
'http://111.93.2.38:8080/gsp/gsp/authenticate?grant_type=token' \
-H 'cache-control: no-cache' \
-H 'gspappid: 64AB7894D61644F3B5CA18B4738C700D' \
-H 'gspappsecret: 70988876GEEEEG4504GB400G92B801E23686' \
PHP Code:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "8080",
CURLOPT_URL => "http://111.93.2.38:8080/gsp/gsp/authenticate?grant_type=token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"gspappid: 64AB7894D61644F3B5CA18B4738C700D",
"gspappsecret: 70988876GEEEEG4504GB400G92B801E23686",
"postman-token: ff8278bb-b888-9fd3-6a55-780dafd58aa5"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Please help me.
What kind of error are you facing , can you please share so we can help you?
If there is timeout error , you can try changing value of CURLOPT_TIMEOUT => 30

PHP Curl Script Working in browser, but doesn't trigger in a function

Absolutely baffled by this problem. I have a curl script, when pasted in a separate file, put the url of the file in a browser and hit enter, it works. But when the same script is to be triggered under a function. It doesn't run.
The script sends card details for payment processing e.g. It sends details to mastercard which deducts pay and returns a response to be acted upon
Here is the script (NOTE I have changed some details for privacy):
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://cardsapi.example.com/step1.php?cvv=467&cardno=123456789011&ccexpmonth=02&ccexpyear=18&amount=15000&email=example#example.com&names=TestUser&transaction_id=111111111&callback=http://cardsapi.example.com",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer UVZoQk9rcFhDcWxlR1RPSnJBa1RaRkZQSjdZdlVSRzdZTThHVldLUU1jZz06MTIzNDU6UTIydkozVll4RzFMWUV2MkViSDl5UVN3NFFRanZrNVJoVThQM0pXTXRIRT06MTk3LjI0OC4xNDkuNjI6MDQvMDAvMTcgMTIwMA==",
"Cache-Control: no-cache",
"Postman-Token: 98defbc9-f5af-3b48-b416-b9d31325df07",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
),
)
);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
If I put this code in something like curlscript.php and run it ex. http://localhost/scripts/curlscript.php , it works. But when I put the code under a function e.g. send_process_pay() in a file e.g. process_pay.php it doesn't trigger. process_pay.php validates the data, triggers function if ok and acts upon response from MasterCard/VISA etc.
How can I make it run under a function?
EDIT:
This is how it is housed in a function and called:
function send_process_pay(){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://cardsapi.example.com/step1.php?cvv=467&cardno=123456789011&ccexpmonth=02&ccexpyear=18&amount=15000&email=example#example.com&names=TestUser&transaction_id=111111111&callback=http://cardsapi.example.com",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer UVZoQk9rcFhDcWxlR1RPSnJBa1RaRkZQSjdZdlVSRzdZTThHVldLUU1jZz06MTIzNDU6UTIydkozVll4RzFMWUV2MkViSDl5UVN3NFFRanZrNVJoVThQM0pXTXRIRT06MTk3LjI0OC4xNDkuNjI6MDQvMDAvMTcgMTIwMA==",
"Cache-Control: no-cache",
"Postman-Token: 98defbc9-f5af-3b48-b416-b9d31325df07",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
),
)
);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
return $response;
}
}
and then:
if(data_valid){
$the_response = send_process_pay();
}
So when the data is valid, the response is stored in a variable.So other proccesses are activated depending on response
Update:
Having had a look at the code, the cURL script is indeed working. The difference between running the file over putting it as a function is that, when running a file, the response, which is html with a javascript auto-submit executes but in the function, the reponse (html) is returned but not executed.
The problem now is to figure out how to run the response so that the auto-submit javascript function in it works.

Categories