Convert cURL Command Line to cURL PHP with URL Encoded











up vote
1
down vote

favorite












I am trying to convert a command line API to PHP cURL



Here's command line php curl



curl --data-urlencode "type=5" --data-urlencode "url=https://example.com/test.torrent" --data-urlencode "comment=API test" --data-urlencode "website=https://example.com/" --data-urlencode "apikey=123456-asdfasdfasdfasdfasdfasdf" --data-urlencode "send=true" https://www.source-site.com/get.php


This is what I am trying with PHP



<?php
ini_set('display_errors',1);
$arr=array(
'type' => 'category5',
'url' => 'https://example.com/test.torrent',
'website' => 'https://example.com',
'apikey' => '696c1a998ac8f13154-088605f-9b0',
'send' => 'true'
);
$data_string = http_build_query($arr);
$ch = curl_init('https://www.source-site.com/get.php'); // this url where my file will be uploaded
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
echo curl_error($ch);
?>


Now the problem is, it does not upload the file nor display any errors the page is blank, what's wrong with my PHP codes?



I have updated my codes now I get the following error



error setting certificate verify locations: CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none









share|improve this question




















  • 2




    If you look for errors curl_error($ch) Then maybe you will see them
    – RiggsFolly
    Nov 19 at 16:25








  • 1




    You are sending url encoded form data Content-Type, but your data is JSON encoded.
    – DanFromGermany
    Nov 19 at 16:26






  • 1




    You're probably looking for http_build_query rather than json_decode
    – iainn
    Nov 19 at 16:27










  • Ok checked the curl_error($ch) and http_build_query but still page is blank
    – Ken
    Nov 19 at 16:30












  • in that script you have a syntax error. there's an extra ` );` on the line with CURLOPT_HTTPHEADER. Actually the second argument to that is supposed to be an array of strings
    – Brice
    Nov 19 at 16:48

















up vote
1
down vote

favorite












I am trying to convert a command line API to PHP cURL



Here's command line php curl



curl --data-urlencode "type=5" --data-urlencode "url=https://example.com/test.torrent" --data-urlencode "comment=API test" --data-urlencode "website=https://example.com/" --data-urlencode "apikey=123456-asdfasdfasdfasdfasdfasdf" --data-urlencode "send=true" https://www.source-site.com/get.php


This is what I am trying with PHP



<?php
ini_set('display_errors',1);
$arr=array(
'type' => 'category5',
'url' => 'https://example.com/test.torrent',
'website' => 'https://example.com',
'apikey' => '696c1a998ac8f13154-088605f-9b0',
'send' => 'true'
);
$data_string = http_build_query($arr);
$ch = curl_init('https://www.source-site.com/get.php'); // this url where my file will be uploaded
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
echo curl_error($ch);
?>


Now the problem is, it does not upload the file nor display any errors the page is blank, what's wrong with my PHP codes?



I have updated my codes now I get the following error



error setting certificate verify locations: CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none









share|improve this question




















  • 2




    If you look for errors curl_error($ch) Then maybe you will see them
    – RiggsFolly
    Nov 19 at 16:25








  • 1




    You are sending url encoded form data Content-Type, but your data is JSON encoded.
    – DanFromGermany
    Nov 19 at 16:26






  • 1




    You're probably looking for http_build_query rather than json_decode
    – iainn
    Nov 19 at 16:27










  • Ok checked the curl_error($ch) and http_build_query but still page is blank
    – Ken
    Nov 19 at 16:30












  • in that script you have a syntax error. there's an extra ` );` on the line with CURLOPT_HTTPHEADER. Actually the second argument to that is supposed to be an array of strings
    – Brice
    Nov 19 at 16:48















up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am trying to convert a command line API to PHP cURL



Here's command line php curl



curl --data-urlencode "type=5" --data-urlencode "url=https://example.com/test.torrent" --data-urlencode "comment=API test" --data-urlencode "website=https://example.com/" --data-urlencode "apikey=123456-asdfasdfasdfasdfasdfasdf" --data-urlencode "send=true" https://www.source-site.com/get.php


This is what I am trying with PHP



<?php
ini_set('display_errors',1);
$arr=array(
'type' => 'category5',
'url' => 'https://example.com/test.torrent',
'website' => 'https://example.com',
'apikey' => '696c1a998ac8f13154-088605f-9b0',
'send' => 'true'
);
$data_string = http_build_query($arr);
$ch = curl_init('https://www.source-site.com/get.php'); // this url where my file will be uploaded
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
echo curl_error($ch);
?>


Now the problem is, it does not upload the file nor display any errors the page is blank, what's wrong with my PHP codes?



I have updated my codes now I get the following error



error setting certificate verify locations: CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none









share|improve this question















I am trying to convert a command line API to PHP cURL



Here's command line php curl



curl --data-urlencode "type=5" --data-urlencode "url=https://example.com/test.torrent" --data-urlencode "comment=API test" --data-urlencode "website=https://example.com/" --data-urlencode "apikey=123456-asdfasdfasdfasdfasdfasdf" --data-urlencode "send=true" https://www.source-site.com/get.php


This is what I am trying with PHP



<?php
ini_set('display_errors',1);
$arr=array(
'type' => 'category5',
'url' => 'https://example.com/test.torrent',
'website' => 'https://example.com',
'apikey' => '696c1a998ac8f13154-088605f-9b0',
'send' => 'true'
);
$data_string = http_build_query($arr);
$ch = curl_init('https://www.source-site.com/get.php'); // this url where my file will be uploaded
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
echo curl_error($ch);
?>


Now the problem is, it does not upload the file nor display any errors the page is blank, what's wrong with my PHP codes?



I have updated my codes now I get the following error



error setting certificate verify locations: CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none






php curl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 16:53

























asked Nov 19 at 16:23









Ken

217




217








  • 2




    If you look for errors curl_error($ch) Then maybe you will see them
    – RiggsFolly
    Nov 19 at 16:25








  • 1




    You are sending url encoded form data Content-Type, but your data is JSON encoded.
    – DanFromGermany
    Nov 19 at 16:26






  • 1




    You're probably looking for http_build_query rather than json_decode
    – iainn
    Nov 19 at 16:27










  • Ok checked the curl_error($ch) and http_build_query but still page is blank
    – Ken
    Nov 19 at 16:30












  • in that script you have a syntax error. there's an extra ` );` on the line with CURLOPT_HTTPHEADER. Actually the second argument to that is supposed to be an array of strings
    – Brice
    Nov 19 at 16:48
















  • 2




    If you look for errors curl_error($ch) Then maybe you will see them
    – RiggsFolly
    Nov 19 at 16:25








  • 1




    You are sending url encoded form data Content-Type, but your data is JSON encoded.
    – DanFromGermany
    Nov 19 at 16:26






  • 1




    You're probably looking for http_build_query rather than json_decode
    – iainn
    Nov 19 at 16:27










  • Ok checked the curl_error($ch) and http_build_query but still page is blank
    – Ken
    Nov 19 at 16:30












  • in that script you have a syntax error. there's an extra ` );` on the line with CURLOPT_HTTPHEADER. Actually the second argument to that is supposed to be an array of strings
    – Brice
    Nov 19 at 16:48










2




2




If you look for errors curl_error($ch) Then maybe you will see them
– RiggsFolly
Nov 19 at 16:25






If you look for errors curl_error($ch) Then maybe you will see them
– RiggsFolly
Nov 19 at 16:25






1




1




You are sending url encoded form data Content-Type, but your data is JSON encoded.
– DanFromGermany
Nov 19 at 16:26




You are sending url encoded form data Content-Type, but your data is JSON encoded.
– DanFromGermany
Nov 19 at 16:26




1




1




You're probably looking for http_build_query rather than json_decode
– iainn
Nov 19 at 16:27




You're probably looking for http_build_query rather than json_decode
– iainn
Nov 19 at 16:27












Ok checked the curl_error($ch) and http_build_query but still page is blank
– Ken
Nov 19 at 16:30






Ok checked the curl_error($ch) and http_build_query but still page is blank
– Ken
Nov 19 at 16:30














in that script you have a syntax error. there's an extra ` );` on the line with CURLOPT_HTTPHEADER. Actually the second argument to that is supposed to be an array of strings
– Brice
Nov 19 at 16:48






in that script you have a syntax error. there's an extra ` );` on the line with CURLOPT_HTTPHEADER. Actually the second argument to that is supposed to be an array of strings
– Brice
Nov 19 at 16:48














1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










I replicated your code and had my own script that echo'd the $_POST array. I curlopt_custom_request to CURLOPT_POST set to true and added CURLOPT_FOLLOWLOCATION. I notice that your url has https as well which if your test site doesn't have a cert will cause this to fail you can try setting CURLOPT_SSL_VERIFYPEER to false for testing, but I didn't have much luck with that. So if that doesn't work maybe try http://yoursite.com.



<?php
ini_set('display_errors',1);
$arr=array(
'type' => 'category5',
'url' => 'https://example.com/test.torrent',
'website' => 'https://example.com',
'apikey' => '696c1a998ac8f13154-088605f-9b0',
'send' => 'true'
);
$data_string = http_build_query($arr);
echo $data_string;
$ch = curl_init('YOUR URL HERE'); // this url where my file will be uploaded
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
$final = curl_exec($ch);
var_dump($final);
?>





share|improve this answer





















  • Thanks Brice it worked thanks a lot
    – Ken
    Nov 19 at 17:27










  • did you just use http for your url or did https still work? @Ken
    – Brice
    Nov 19 at 17:39












  • https works with curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); and if you think my question was useful so please vote up to my question and thanks once again
    – Ken
    Nov 19 at 17:43











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378824%2fconvert-curl-command-line-to-curl-php-with-url-encoded%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










I replicated your code and had my own script that echo'd the $_POST array. I curlopt_custom_request to CURLOPT_POST set to true and added CURLOPT_FOLLOWLOCATION. I notice that your url has https as well which if your test site doesn't have a cert will cause this to fail you can try setting CURLOPT_SSL_VERIFYPEER to false for testing, but I didn't have much luck with that. So if that doesn't work maybe try http://yoursite.com.



<?php
ini_set('display_errors',1);
$arr=array(
'type' => 'category5',
'url' => 'https://example.com/test.torrent',
'website' => 'https://example.com',
'apikey' => '696c1a998ac8f13154-088605f-9b0',
'send' => 'true'
);
$data_string = http_build_query($arr);
echo $data_string;
$ch = curl_init('YOUR URL HERE'); // this url where my file will be uploaded
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
$final = curl_exec($ch);
var_dump($final);
?>





share|improve this answer





















  • Thanks Brice it worked thanks a lot
    – Ken
    Nov 19 at 17:27










  • did you just use http for your url or did https still work? @Ken
    – Brice
    Nov 19 at 17:39












  • https works with curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); and if you think my question was useful so please vote up to my question and thanks once again
    – Ken
    Nov 19 at 17:43















up vote
0
down vote



accepted










I replicated your code and had my own script that echo'd the $_POST array. I curlopt_custom_request to CURLOPT_POST set to true and added CURLOPT_FOLLOWLOCATION. I notice that your url has https as well which if your test site doesn't have a cert will cause this to fail you can try setting CURLOPT_SSL_VERIFYPEER to false for testing, but I didn't have much luck with that. So if that doesn't work maybe try http://yoursite.com.



<?php
ini_set('display_errors',1);
$arr=array(
'type' => 'category5',
'url' => 'https://example.com/test.torrent',
'website' => 'https://example.com',
'apikey' => '696c1a998ac8f13154-088605f-9b0',
'send' => 'true'
);
$data_string = http_build_query($arr);
echo $data_string;
$ch = curl_init('YOUR URL HERE'); // this url where my file will be uploaded
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
$final = curl_exec($ch);
var_dump($final);
?>





share|improve this answer





















  • Thanks Brice it worked thanks a lot
    – Ken
    Nov 19 at 17:27










  • did you just use http for your url or did https still work? @Ken
    – Brice
    Nov 19 at 17:39












  • https works with curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); and if you think my question was useful so please vote up to my question and thanks once again
    – Ken
    Nov 19 at 17:43













up vote
0
down vote



accepted







up vote
0
down vote



accepted






I replicated your code and had my own script that echo'd the $_POST array. I curlopt_custom_request to CURLOPT_POST set to true and added CURLOPT_FOLLOWLOCATION. I notice that your url has https as well which if your test site doesn't have a cert will cause this to fail you can try setting CURLOPT_SSL_VERIFYPEER to false for testing, but I didn't have much luck with that. So if that doesn't work maybe try http://yoursite.com.



<?php
ini_set('display_errors',1);
$arr=array(
'type' => 'category5',
'url' => 'https://example.com/test.torrent',
'website' => 'https://example.com',
'apikey' => '696c1a998ac8f13154-088605f-9b0',
'send' => 'true'
);
$data_string = http_build_query($arr);
echo $data_string;
$ch = curl_init('YOUR URL HERE'); // this url where my file will be uploaded
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
$final = curl_exec($ch);
var_dump($final);
?>





share|improve this answer












I replicated your code and had my own script that echo'd the $_POST array. I curlopt_custom_request to CURLOPT_POST set to true and added CURLOPT_FOLLOWLOCATION. I notice that your url has https as well which if your test site doesn't have a cert will cause this to fail you can try setting CURLOPT_SSL_VERIFYPEER to false for testing, but I didn't have much luck with that. So if that doesn't work maybe try http://yoursite.com.



<?php
ini_set('display_errors',1);
$arr=array(
'type' => 'category5',
'url' => 'https://example.com/test.torrent',
'website' => 'https://example.com',
'apikey' => '696c1a998ac8f13154-088605f-9b0',
'send' => 'true'
);
$data_string = http_build_query($arr);
echo $data_string;
$ch = curl_init('YOUR URL HERE'); // this url where my file will be uploaded
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
$final = curl_exec($ch);
var_dump($final);
?>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 17:01









Brice

396211




396211












  • Thanks Brice it worked thanks a lot
    – Ken
    Nov 19 at 17:27










  • did you just use http for your url or did https still work? @Ken
    – Brice
    Nov 19 at 17:39












  • https works with curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); and if you think my question was useful so please vote up to my question and thanks once again
    – Ken
    Nov 19 at 17:43


















  • Thanks Brice it worked thanks a lot
    – Ken
    Nov 19 at 17:27










  • did you just use http for your url or did https still work? @Ken
    – Brice
    Nov 19 at 17:39












  • https works with curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); and if you think my question was useful so please vote up to my question and thanks once again
    – Ken
    Nov 19 at 17:43
















Thanks Brice it worked thanks a lot
– Ken
Nov 19 at 17:27




Thanks Brice it worked thanks a lot
– Ken
Nov 19 at 17:27












did you just use http for your url or did https still work? @Ken
– Brice
Nov 19 at 17:39






did you just use http for your url or did https still work? @Ken
– Brice
Nov 19 at 17:39














https works with curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); and if you think my question was useful so please vote up to my question and thanks once again
– Ken
Nov 19 at 17:43




https works with curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); and if you think my question was useful so please vote up to my question and thanks once again
– Ken
Nov 19 at 17:43


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378824%2fconvert-curl-command-line-to-curl-php-with-url-encoded%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]