Home > OS >  CMD line vs PHP script
CMD line vs PHP script

Time:09-15

I can run a curl command in PowerShell and it works, but does not when I try to move to a php script (php also run from the command line) Command line:

curl.exe https://api.bigcommerce.com/stores/a1b2c3d4/v3/customers/attributes -H "x-auth-token: a1b2c3d4a1b2c3d4a1b2c3d4a1b2c3d4" -H "Content-Type: application/json"

Returns:

{"data":[{"id":2,"name":"gp_customer_id","type":"string","date_created":"2021-12-14T14:18:10Z","date_modified":"2021-12-14T14:18:10Z"}],"meta":{"pagination":{"total":1,"count":1,"per_page":50,"current_page":1,"total_pages":1}}}
PS C:\Users\JAMESB\Documents\webroot\BC\swamp

PHP file:

$ACCESS_TOKEN = "a1b2c3d4a1b2c3d4a1b2c3d4a1b2c3d4";
$STORE_HASH = "a1b2c3d4";

$headers = array();
$headers[] = "x-auth-token: $ACCESS_TOKEN";
$headers[] = 'Content-Type: application/json';
$headers[] = 'ACCEPT: application/json';
var_dump ($headers);
    echo "https://api.bigcommerce.com/stores/$STORE_HASH/v3/customers/attributes\n";
$state_ch = curl_init();
//    curl_setopt($state_ch, CURLOPT_URL,"https://api.bigcommerce.com/stores/$STORE_HASH/v3/customers/attributes");
    curl_setopt($state_ch, CURLOPT_URL,"https://api.bigcommerce.com/stores/a1b2c3d4/v3/customers/attributes");
    curl_setopt($state_ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($state_ch, CURLOPT_HTTPHEADER, $headers);
var_dump($state_ch);
    $state_result = curl_exec ($state_ch);
var_dump($state_result);
    $state_result = json_decode($state_result);

var_dump($state_result);
?>

Returns:

array(3) {
[0]=>   string(45) "x-auth-token: a1b2c3d4a1b2c3d4a1b2c3d4a1b2c3d4"
[1]=>   string(30) "Content-Type: application/json"     
[2]=>   string(24) "ACCEPT: application/json"}
https://api.bigcommerce.com/stores/a1b2c3d4/v3/customers/attributes  resource(4) of type (curl)  bool(false)  NULL

Thanks! Jimbus

CodePudding user response:

curl_exec() is failing (it returned false)

Try calling curl_error() to see why:

$state_result = curl_exec ($state_ch);
if ( ! $state_result) {
    print(curl_error($state_ch) . PHP_EOL);
}

Note: The "ACCEPT" header may not be correct. I've never seen any in all caps, and I can't remember what the specs say about that. Also, you don't show an ACCEPT header in your command-line version.

CodePudding user response:

DaveAM's answer got me to an error:

SSL certificate problem: unable to get local issuer certificate

From there, another stackoverflow answer had this:

The above steps, though helpful, didnt work for me on Windows 8. I don't know the co-relation, but the below steps worked. Basically a change in the cacert.pem file. Hope this helps someone.

Download cacert.pem file from here: http://curl.haxx.se/docs/caextract.html Save the file in your PHP installation folder. (eg: If using xampp – save it in c:\Installation_Dir\xampp\php\cacert.pem). Open your php.ini file and add these lines: curl.cainfo=”C:\Installation_Dir\xampp\php\cacert.pem” openssl.cafile=”C:\Installation_Dir\xampp\php\cacert.pem” Restart your Apache server and that should fix it (Simply stop and start the services as needed).

Which worked beautifully! I'm a newbie and don't have enough rep to like Dave's answer. I'd appreciate if if you'd show him some love :)

  • Related